default, required added

This commit is contained in:
crusader 2018-09-04 12:19:41 +09:00
parent 430e8dc498
commit 178b12d303
4 changed files with 89 additions and 7 deletions

21
Gopkg.lock generated Normal file
View File

@ -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

38
Gopkg.toml Normal file
View File

@ -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

View File

@ -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
}

View File

@ -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)
})
}
}