-
Notifications
You must be signed in to change notification settings - Fork 73
Add user-defined tag naming #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'd also like to add minimum specs for testing user-specified tag names. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,25 +6,38 @@ import ( | |
| "errors" | ||
| "reflect" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| var ( | ||
| errInvalidTag = errors.New("invalid tag name") | ||
| errInvalidType = errors.New("not a struct pointer") | ||
| ) | ||
|
|
||
| const ( | ||
| fieldName = "default" | ||
| ) | ||
|
|
||
| // Set initializes members in a struct referenced by a pointer. | ||
| // Maps and slices are initialized by `make` and other primitive types are set with default values. | ||
| // `ptr` should be a struct pointer | ||
| func Set(ptr interface{}) error { | ||
| func Set(ptr interface{}) (err error) { | ||
|
|
||
| err = SetWithTag(ptr, "default") | ||
|
|
||
| return | ||
| } | ||
|
|
||
| // SetWithTag initializes members in a struct referenced by a pointer using an explicit tag name. | ||
| // Maps and slices are initialized by `make` and other primitive types are set with default values. | ||
| // `ptr` should be a struct pointer | ||
| func SetWithTag(ptr interface{}, runtimeTag string) error { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
|
|
||
| if reflect.TypeOf(ptr).Kind() != reflect.Ptr { | ||
| return errInvalidType | ||
| } | ||
|
|
||
| if strings.TrimSpace(runtimeTag) == "" { | ||
| return errInvalidTag | ||
| } | ||
|
|
||
| v := reflect.ValueOf(ptr).Elem() | ||
| t := v.Type() | ||
|
|
||
|
|
@@ -33,8 +46,8 @@ func Set(ptr interface{}) error { | |
| } | ||
|
|
||
| for i := 0; i < t.NumField(); i++ { | ||
| if defaultVal := t.Field(i).Tag.Get(fieldName); defaultVal != "-" { | ||
| if err := setField(v.Field(i), defaultVal); err != nil { | ||
| if defaultVal := t.Field(i).Tag.Get(runtimeTag); defaultVal != "-" { | ||
| if err := setField(v.Field(i), defaultVal, runtimeTag); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
@@ -51,7 +64,15 @@ func MustSet(ptr interface{}) { | |
| } | ||
| } | ||
|
|
||
| func setField(field reflect.Value, defaultVal string) error { | ||
| // MustSetWithTag function is a wrapper of SetWithTag function | ||
| // It will call Set and panic if err not equals nil. | ||
| func MustSetWithTag(ptr interface{}, runtimeTag string) { | ||
| if err := SetWithTag(ptr, runtimeTag); err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
|
|
||
| func setField(field reflect.Value, defaultVal string, tagName string) error { | ||
| if !field.CanSet() { | ||
| return nil | ||
| } | ||
|
|
@@ -160,16 +181,16 @@ func setField(field reflect.Value, defaultVal string) error { | |
| switch field.Kind() { | ||
| case reflect.Ptr: | ||
| if isInitial || field.Elem().Kind() == reflect.Struct { | ||
| setField(field.Elem(), defaultVal) | ||
| setField(field.Elem(), defaultVal, tagName) | ||
| callSetter(field.Interface()) | ||
| } | ||
| case reflect.Struct: | ||
| if err := Set(field.Addr().Interface()); err != nil { | ||
| if err := SetWithTag(field.Addr().Interface(), tagName); err != nil { | ||
| return err | ||
| } | ||
| case reflect.Slice: | ||
| for j := 0; j < field.Len(); j++ { | ||
| if err := setField(field.Index(j), ""); err != nil { | ||
| if err := setField(field.Index(j), "", tagName); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
@@ -181,14 +202,14 @@ func setField(field reflect.Value, defaultVal string) error { | |
| case reflect.Ptr: | ||
| switch v.Elem().Kind() { | ||
| case reflect.Struct, reflect.Slice, reflect.Map: | ||
| if err := setField(v.Elem(), ""); err != nil { | ||
| if err := setField(v.Elem(), "", tagName); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| case reflect.Struct, reflect.Slice, reflect.Map: | ||
| ref := reflect.New(v.Type()) | ||
| ref.Elem().Set(v) | ||
| if err := setField(ref.Elem(), ""); err != nil { | ||
| if err := setField(ref.Elem(), "", tagName); err != nil { | ||
| return err | ||
| } | ||
| field.SetMapIndex(e, ref.Elem().Convert(v.Type())) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you please revert these changes?
"User-Definable tag name" should not be this library's primary use case.
The same goes for example/main.go — Although it might be useful to have a separate file.