From e1155e17b0dbe4971222f526185cceacdb7f452d Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Sun, 5 Nov 2023 12:36:53 -0800 Subject: [PATCH] add back depguard - yet https://github.com/OpenPeeDeeP/depguard/issues/66. add note about go version(s) --- .golangci.yml | 1 - README.md | 6 ++++-- env_test.go | 20 +++++++++----------- go.mod | 4 +++- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 3847205..e768a73 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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 (!) diff --git a/README.md b/README.md index ef6e7f8..7ced03d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/env_test.go b/env_test.go index f37c481..d1049db 100644 --- a/env_test.go +++ b/env_test.go @@ -1,12 +1,10 @@ -package struct2env_test +package struct2env import ( "os" "reflect" "strings" "testing" - - "fortio.org/struct2env" ) func TestSplitByCase(t *testing.T) { @@ -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) } @@ -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) } } @@ -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) } } @@ -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" @@ -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) } diff --git a/go.mod b/go.mod index adfd05e..83093a6 100644 --- a/go.mod +++ b/go.mod @@ -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