From 178b12d303bdd83566a35d71cce68b0c6cc1f4d3 Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 4 Sep 2018 12:19:41 +0900 Subject: [PATCH] default, required added --- Gopkg.lock | 21 +++++++++++++++++++++ Gopkg.toml | 38 ++++++++++++++++++++++++++++++++++++++ registry.go | 23 +++++++++++++++++++++++ registry_test.go | 14 +++++++------- 4 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..e6ce16f --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,21 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + branch = "master" + name = "git.loafle.net/overflow/util-go" + packages = ["reflect"] + revision = "fae2846a85aad314ee44957d428478fbe800045f" + +[[projects]] + name = "gopkg.in/yaml.v2" + packages = ["."] + revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183" + version = "v2.2.1" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "8c01a65650b6ec8ab959868d85ad4a1a857328764f44e27b654d8916c1f65d6f" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..ab853ab --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,38 @@ +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true + + +[[constraint]] + branch = "master" + name = "git.loafle.net/overflow/util-go" + +[[constraint]] + name = "gopkg.in/yaml.v2" + version = "2.2.1" + +[prune] + go-tests = true + unused-packages = true diff --git a/registry.go b/registry.go index ca549c3..dbbe991 100644 --- a/registry.go +++ b/registry.go @@ -7,6 +7,7 @@ import ( "strings" our "git.loafle.net/overflow/util-go/reflect" + yaml "gopkg.in/yaml.v2" ) type Registry interface { @@ -160,6 +161,28 @@ func (r *AnnotationRegistry) getAnnotation(f *reflect.StructField) (map[reflect. } } + _v := reflect.Indirect(reflect.ValueOf(i)) + for index := 0; index < _v.NumField(); index++ { + _f := _v.Field(index) + _fs := def.rt.Field(index) + + if !_f.CanAddr() || !_f.CanInterface() { + continue + } + + if isBlank := reflect.DeepEqual(_f.Interface(), reflect.Zero(_f.Type()).Interface()); isBlank { + // Set default configuration if blank + if value := _fs.Tag.Get(DefaultTag); value != "" { + if err := yaml.Unmarshal([]byte(value), _f.Addr().Interface()); err != nil { + return nil, fmt.Errorf("Unmarshal failed %v", err) + } + } else if _fs.Tag.Get(RequiredTag) == "true" { + // return error if it is required but blank + return nil, fmt.Errorf("%s is required, but blank", _fs.Name) + } + } + } + annotations[def.t] = i } diff --git a/registry_test.go b/registry_test.go index 48fe6bd..8b91140 100644 --- a/registry_test.go +++ b/registry_test.go @@ -3,18 +3,13 @@ package annotation import ( "reflect" "testing" - - di "git.loafle.net/overflow/di-go" ) var InjectableAnnotationType = reflect.TypeOf((*InjectableAnnotation)(nil)) type InjectableAnnotation struct { - Annotation `@name:"@Injectable"` - Name string `json:"name" @default:""` - InitMethod string `json:"initMethod"` - DestroyMethod string `json:"destroyMethod"` - Scope di.ScopeType `json:"scope"` + Annotation `@name:"@Injectable"` + Name string `json:"name" @default:"" @required:"false"` } var InjectableServiceType = reflect.TypeOf((*InjectableService)(nil)) @@ -102,6 +97,11 @@ func TestAnnotationRegistry_Register(t *testing.T) { if err := r.Register(tt.args.t); (err != nil) != tt.wantErr { t.Errorf("AnnotationRegistry.Register() error = %v, wantErr %v", err, tt.wantErr) } + a, err := r.GetTypeAnnotation(InjectableServiceType, InjectableAnnotationType) + if nil != err { + t.Error(err) + } + t.Log(a) }) } }