-
Notifications
You must be signed in to change notification settings - Fork 74
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 2 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,17 +6,23 @@ | |
| "errors" | ||
| "reflect" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| var ( | ||
| errInvalidTag = errors.New("invalid tag name") | ||
| errInvalidType = errors.New("not a struct pointer") | ||
| ) | ||
|
|
||
| const ( | ||
| fieldName = "default" | ||
| ) | ||
|
|
||
| var ( | ||
| TagName string = fieldName | ||
| ) | ||
johnnybubonic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // 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 | ||
|
|
@@ -25,6 +31,40 @@ | |
| return errInvalidType | ||
| } | ||
|
|
||
| if strings.TrimSpace(TagName) == "" { | ||
| return errInvalidTag | ||
| } | ||
|
|
||
| v := reflect.ValueOf(ptr).Elem() | ||
| t := v.Type() | ||
|
|
||
| if t.Kind() != reflect.Struct { | ||
| return errInvalidType | ||
| } | ||
|
|
||
| for i := 0; i < t.NumField(); i++ { | ||
| if defaultVal := t.Field(i).Tag.Get(TagName); defaultVal != "-" { | ||
| if err := setField(v.Field(i), defaultVal); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
| callSetter(ptr) | ||
| return nil | ||
| } | ||
|
|
||
| // 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,7 +73,7 @@ | |
| } | ||
|
|
||
| for i := 0; i < t.NumField(); i++ { | ||
| if defaultVal := t.Field(i).Tag.Get(fieldName); defaultVal != "-" { | ||
| if defaultVal := t.Field(i).Tag.Get(runtimeTag); defaultVal != "-" { | ||
| if err := setField(v.Field(i), defaultVal); err != nil { | ||
| return err | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.