From 008af573a8f4d0c0d3fe200cae89d1306602eb4f Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Thu, 13 Aug 2020 12:46:27 +0300 Subject: [PATCH 1/6] HW8.1 is completed --- .golangci.yml | 1 + hw08_zenv_to_structure/README.md | 3 ++ hw08_zenv_to_structure/env_to_struct.go | 57 ++++++++++++++++++++ hw08_zenv_to_structure/env_to_struct_test.go | 46 ++++++++++++++++ hw08_zenv_to_structure/go.mod | 8 +++ 5 files changed, 115 insertions(+) create mode 100644 hw08_zenv_to_structure/README.md create mode 100644 hw08_zenv_to_structure/env_to_struct.go create mode 100644 hw08_zenv_to_structure/env_to_struct_test.go create mode 100644 hw08_zenv_to_structure/go.mod diff --git a/.golangci.yml b/.golangci.yml index b9f4356..a353259 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -15,3 +15,4 @@ linters: - wsl - gofumpt - gosec + - nlreturn \ No newline at end of file diff --git a/hw08_zenv_to_structure/README.md b/hw08_zenv_to_structure/README.md new file mode 100644 index 0000000..7ff272d --- /dev/null +++ b/hw08_zenv_to_structure/README.md @@ -0,0 +1,3 @@ +## Домашнее задание №8_1 «Рефлексия» + +Реализовать функцию, заполняющую структуру значениями из переменных окружения. diff --git a/hw08_zenv_to_structure/env_to_struct.go b/hw08_zenv_to_structure/env_to_struct.go new file mode 100644 index 0000000..737028d --- /dev/null +++ b/hw08_zenv_to_structure/env_to_struct.go @@ -0,0 +1,57 @@ +package hw08_zenv_to_structure //nolint:golint,stylecheck +import ( + "fmt" + "log" + "os" + "reflect" + "strconv" + "strings" +) + +func Slice2struct(mp []string, str interface{}) error { + v := reflect.ValueOf(str) + if v.Kind() != reflect.Ptr { + return fmt.Errorf("%T is not a pointer", str) + } + v = v.Elem() + if v.Kind() != reflect.Struct { + return fmt.Errorf("%T is not a pointer to struct", str) + } + + for _, e := range mp { + st := strings.Split(e, "=") + var key string = st[0] + var value interface{} = st[1] + f := v.FieldByName(key) + if f.IsValid() && f.CanSet() { + switch v := f.Interface().(type) { + case int: + val, err := strconv.Atoi(value.(string)) + if err != nil { + return err + } + x := int64(val) + if !f.OverflowInt(x) { + f.SetInt(x) + } + case string: + f.SetString(value.(string)) + case bool: + f.SetBool(value.(string) == "true") + case []interface{}: + input := reflect.ValueOf(value) + f.Set(input) + default: + return fmt.Errorf("i don't know how to parse type %T", v) + } + } + } + return nil +} + +func Env2struct(s interface{}) { + env := os.Environ() + if err := Slice2struct(env, s); err != nil { + log.Fatal("ошибка", err.Error()) + } +} diff --git a/hw08_zenv_to_structure/env_to_struct_test.go b/hw08_zenv_to_structure/env_to_struct_test.go new file mode 100644 index 0000000..7944807 --- /dev/null +++ b/hw08_zenv_to_structure/env_to_struct_test.go @@ -0,0 +1,46 @@ +package hw08_zenv_to_structure + +import ( + "github.com/stretchr/testify/require" + "log" + "os" + "testing" +) + +func TestSlice2struct(t *testing.T) { + mp := []string{"ENV1=fsfsdvsvssdfsd fsf fsf sds", "ENV2=12345", "ENV3=aefrsgrgdtgtdhn"} + var st struct { + ENV1 string + ENV2 int + ENV3 string + } + err := Slice2struct(mp, &st) + require.Equal(t, struct { + ENV1 string + ENV2 int + ENV3 string + }{"fsfsdvsvssdfsd fsf fsf sds", 12345, "aefrsgrgdtgtdhn"}, st) + require.NoError(t, err) +} + +func TestEnv2struct(t *testing.T) { + type env struct { + ENV1 string + ENV2 bool + ENV3 int + } + exp := env{"какая-то строка", true, 1234567} + if os.Setenv("ENV1", "какая-то строка") != nil { + log.Fatal("не удалось установить ENV") + } + if os.Setenv("ENV2", "true") != nil { + log.Fatal("не удалось установить ENV") + } + if os.Setenv("ENV3", "1234567") != nil { + log.Fatal("не удалось установить ENV") + } + + var res env + Env2struct(&res) + require.Equal(t, exp, res) +} diff --git a/hw08_zenv_to_structure/go.mod b/hw08_zenv_to_structure/go.mod new file mode 100644 index 0000000..00d66d4 --- /dev/null +++ b/hw08_zenv_to_structure/go.mod @@ -0,0 +1,8 @@ +module hw08_zenv_to_structure + +go 1.14 + +require ( + github.com/Ompluscator/dynamic-struct v1.2.0 + github.com/stretchr/testify v1.6.1 +) From 4f2b3c7ca5370073387f904d65da0b505c8309b1 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Thu, 13 Aug 2020 12:49:46 +0300 Subject: [PATCH 2/6] HW8.1 is completed --- hw08_zenv_to_structure/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw08_zenv_to_structure/go.mod b/hw08_zenv_to_structure/go.mod index 00d66d4..6e0abba 100644 --- a/hw08_zenv_to_structure/go.mod +++ b/hw08_zenv_to_structure/go.mod @@ -1,4 +1,4 @@ -module hw08_zenv_to_structure +module github.com/tiburon-777/HW_OTUS/hw08_zenv_to_structure go 1.14 From deb0d84a4685d304221b0f38b8774949c6197872 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Thu, 13 Aug 2020 12:53:05 +0300 Subject: [PATCH 3/6] HW8.1 is completed --- hw08_envdir_tool/go.sum | 10 ++++++++++ hw08_zenv_to_structure/go.mod | 5 +---- hw08_zenv_to_structure/go.sum | 12 ++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 hw08_envdir_tool/go.sum create mode 100644 hw08_zenv_to_structure/go.sum diff --git a/hw08_envdir_tool/go.sum b/hw08_envdir_tool/go.sum new file mode 100644 index 0000000..56d62e7 --- /dev/null +++ b/hw08_envdir_tool/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/hw08_zenv_to_structure/go.mod b/hw08_zenv_to_structure/go.mod index 6e0abba..29e1197 100644 --- a/hw08_zenv_to_structure/go.mod +++ b/hw08_zenv_to_structure/go.mod @@ -2,7 +2,4 @@ module github.com/tiburon-777/HW_OTUS/hw08_zenv_to_structure go 1.14 -require ( - github.com/Ompluscator/dynamic-struct v1.2.0 - github.com/stretchr/testify v1.6.1 -) +require github.com/stretchr/testify v1.6.1 \ No newline at end of file diff --git a/hw08_zenv_to_structure/go.sum b/hw08_zenv_to_structure/go.sum new file mode 100644 index 0000000..f580141 --- /dev/null +++ b/hw08_zenv_to_structure/go.sum @@ -0,0 +1,12 @@ +github.com/Ompluscator/dynamic-struct v1.2.0 h1:6SXShNbjuMKeBIy5LUVx4BI3WKoyugRMchJWC2yjtZA= +github.com/Ompluscator/dynamic-struct v1.2.0/go.mod h1:01g22H1GC9IFcrpQ4JQBkzynp8RoT0wmUMx/OvXNnw8= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From a3ad59da8adfbdc9304ec1adf553d77bbbebba56 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Thu, 13 Aug 2020 13:50:46 +0300 Subject: [PATCH 4/6] HW8.1 is completed --- hw08_zenv_to_structure/go.mod | 5 ----- hw08_zenv_to_structure/go.sum | 12 ------------ 2 files changed, 17 deletions(-) delete mode 100644 hw08_zenv_to_structure/go.mod delete mode 100644 hw08_zenv_to_structure/go.sum diff --git a/hw08_zenv_to_structure/go.mod b/hw08_zenv_to_structure/go.mod deleted file mode 100644 index 29e1197..0000000 --- a/hw08_zenv_to_structure/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module github.com/tiburon-777/HW_OTUS/hw08_zenv_to_structure - -go 1.14 - -require github.com/stretchr/testify v1.6.1 \ No newline at end of file diff --git a/hw08_zenv_to_structure/go.sum b/hw08_zenv_to_structure/go.sum deleted file mode 100644 index f580141..0000000 --- a/hw08_zenv_to_structure/go.sum +++ /dev/null @@ -1,12 +0,0 @@ -github.com/Ompluscator/dynamic-struct v1.2.0 h1:6SXShNbjuMKeBIy5LUVx4BI3WKoyugRMchJWC2yjtZA= -github.com/Ompluscator/dynamic-struct v1.2.0/go.mod h1:01g22H1GC9IFcrpQ4JQBkzynp8RoT0wmUMx/OvXNnw8= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From ded3bbf092b45e346f4c51ce206f7e9ceff33d1e Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Thu, 13 Aug 2020 13:54:13 +0300 Subject: [PATCH 5/6] HW8.1 is completed --- hw08_zenv_to_structure/go.mod | 5 +++++ hw08_zenv_to_structure/go.sum | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 hw08_zenv_to_structure/go.mod create mode 100644 hw08_zenv_to_structure/go.sum diff --git a/hw08_zenv_to_structure/go.mod b/hw08_zenv_to_structure/go.mod new file mode 100644 index 0000000..29e1197 --- /dev/null +++ b/hw08_zenv_to_structure/go.mod @@ -0,0 +1,5 @@ +module github.com/tiburon-777/HW_OTUS/hw08_zenv_to_structure + +go 1.14 + +require github.com/stretchr/testify v1.6.1 \ No newline at end of file diff --git a/hw08_zenv_to_structure/go.sum b/hw08_zenv_to_structure/go.sum new file mode 100644 index 0000000..f580141 --- /dev/null +++ b/hw08_zenv_to_structure/go.sum @@ -0,0 +1,12 @@ +github.com/Ompluscator/dynamic-struct v1.2.0 h1:6SXShNbjuMKeBIy5LUVx4BI3WKoyugRMchJWC2yjtZA= +github.com/Ompluscator/dynamic-struct v1.2.0/go.mod h1:01g22H1GC9IFcrpQ4JQBkzynp8RoT0wmUMx/OvXNnw8= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 960977d60c3442834611543502869c37782b0807 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Thu, 13 Aug 2020 13:59:43 +0300 Subject: [PATCH 6/6] =?UTF-8?q?HW8.1=20is=20completed=20=D0=91=D0=BE=D1=80?= =?UTF-8?q?=D1=8E=D1=81=D1=8C=20=D1=81=20go=20get?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw08_zenv_to_structure/go.sum | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 hw08_zenv_to_structure/go.sum diff --git a/hw08_zenv_to_structure/go.sum b/hw08_zenv_to_structure/go.sum deleted file mode 100644 index f580141..0000000 --- a/hw08_zenv_to_structure/go.sum +++ /dev/null @@ -1,12 +0,0 @@ -github.com/Ompluscator/dynamic-struct v1.2.0 h1:6SXShNbjuMKeBIy5LUVx4BI3WKoyugRMchJWC2yjtZA= -github.com/Ompluscator/dynamic-struct v1.2.0/go.mod h1:01g22H1GC9IFcrpQ4JQBkzynp8RoT0wmUMx/OvXNnw8= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=