Skip to content

Commit

Permalink
add back depguard - yet OpenPeeDeeP/depguard#66. add note about go ve…
Browse files Browse the repository at this point in the history
…rsion(s)
  • Loading branch information
ldemailly committed Nov 5, 2023
1 parent d7f01af commit e1155e1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ linters:
- cyclop
- forcetypeassert
- ireturn
- depguard
enable-all: true
disable-all: false
# Must not use fast: true in newer golangci-lint or it'll just skip a bunch of linter instead of doing caching like before (!)
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ Convert between go structures to environment variable and back (for structured c

There are many go packages that are doing environment to go struct config (for instance https://github.com/kelseyhightower/envconfig) but I didn't find one doing the inverse and we needed to set a bunch of environment variables for shell and other tools to get some configuration structured as JSON and Go object, so this was born. For symetry the reverse was also added (history of commit on https://github.com/fortio/dflag/pull/50/commits)

Standalone package with 0 dependencies outside of the go standard library.
Standalone package with 0 dependencies outside of the go standard library. Developed with go 1.20 but tested with go as old as 1.17
but should works with pretty much any go version, as it only depends on reflection and strconv.

The unit test has a fairly extensive example on how

The unit test has a fairly extensive example on how:
```go
type FooConfig struct {
Foo string
Expand Down
20 changes: 9 additions & 11 deletions env_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package struct2env_test
package struct2env

import (
"os"
"reflect"
"strings"
"testing"

"fortio.org/struct2env"
)

func TestSplitByCase(t *testing.T) {
Expand Down Expand Up @@ -35,7 +33,7 @@ func TestSplitByCase(t *testing.T) {
{"AABbbCcc", []string{"AA", "Bbb", "Ccc"}},
}
for _, test := range tests {
got := struct2env.SplitByCase(test.in)
got := SplitByCase(test.in)
if !reflect.DeepEqual(got, test.out) {
t.Errorf("mismatch for %q: got %v expected %v", test.in, got, test.out)
}
Expand All @@ -62,11 +60,11 @@ func TestCamelCaseToSnakeCase(t *testing.T) {
{"HTTPSServer42", "HTTPS_SERVER42"},
}
for _, test := range tests {
if got := struct2env.CamelCaseToUpperSnakeCase(test.in); got != test.out {
if got := CamelCaseToUpperSnakeCase(test.in); got != test.out {
t.Errorf("for %q expected upper %q and got %q", test.in, test.out, got)
}
lower := strings.ToLower(test.out)
if got := struct2env.CamelCaseToLowerSnakeCase(test.in); got != lower {
if got := CamelCaseToLowerSnakeCase(test.in); got != lower {
t.Errorf("for %q expected lower %q and got %q", test.in, lower, got)
}
}
Expand All @@ -91,7 +89,7 @@ func TestCamelCaseToLowerKebabCase(t *testing.T) {
{"HTTPSServer42", "https-server42"},
}
for _, test := range tests {
if got := struct2env.CamelCaseToLowerKebabCase(test.in); got != test.out {
if got := CamelCaseToLowerKebabCase(test.in); got != test.out {
t.Errorf("for %q expected %q and got %q", test.in, test.out, got)
}
}
Expand Down Expand Up @@ -140,21 +138,21 @@ func TestStructToEnvVars(t *testing.T) {
}
foo.InnerA = "inner a"
foo.InnerB = "inner b"
empty, errors := struct2env.StructToEnvVars(42) // error/empty
empty, errors := StructToEnvVars(42) // error/empty
if len(empty) != 0 {
t.Errorf("expected empty, got %v", empty)
}
if len(errors) != 1 {
t.Errorf("expected errors, got %v", errors)
}
envVars, errors := struct2env.StructToEnvVars(&foo)
envVars, errors := StructToEnvVars(&foo)
if len(errors) != 0 {
t.Errorf("expected no error, got %v", errors)
}
if len(envVars) != 11 {
t.Errorf("expected 11 env vars, got %d: %+v", len(envVars), envVars)
}
str := struct2env.ToShellWithPrefix("TST_", envVars)
str := ToShellWithPrefix("TST_", envVars)
//nolint:lll
expected := `TST_FOO="a\nfoo with \" quotes and \\ and '"
TST_BAR="42str"
Expand Down Expand Up @@ -191,7 +189,7 @@ func TestSetFromEnv(t *testing.T) {
for _, e := range envs {
os.Setenv(e.k, e.v)
}
errors := struct2env.SetFromEnv("TST2_", &foo)
errors := SetFromEnv("TST2_", &foo)
if len(errors) != 0 {
t.Errorf("Unexpectedly got errors :%v", errors)
}
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module fortio.org/struct2env

go 1.20
// Developed with go 1.20 but tested with go as old as 1.17
// but works with pretty much any version, only depends on reflection and strconv
go 1.17

0 comments on commit e1155e1

Please sign in to comment.