Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Initialize structs with default values
- Recursively initializes fields in a struct
- Dynamically sets default values by [`defaults.Setter`](./setter.go) interface
- Preserves non-initial values from being reset with a default value
- User-Definable tag name


Usage
Expand All @@ -42,6 +43,9 @@ import (

type Gender string

// Optionally use a different tag name globally.
//defaults.TagName = "my_custom_tag"

type Sample struct {
Name string `default:"John Smith"`
Age int `default:"27"`
Expand Down Expand Up @@ -83,6 +87,12 @@ func main() {
if err := defaults.Set(obj); err != nil {
panic(err)
}
// Or with a specific tag name (without setting defaults.TagName globally):
/*
if err := defaults.SetWithTag(obj, "some_tag_name"); err != nil {
panic(err)
}
*/

out, err := json.MarshalIndent(obj, "", " ")
if err != nil {
Expand Down
42 changes: 41 additions & 1 deletion defaults.go
Copy link
Owner

Choose a reason for hiding this comment

The 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.
Do note that we don't want to extend the existing struct; create a new struct to test with.

Original file line number Diff line number Diff line change
Expand Up @@ -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
)

// 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
Expand All @@ -25,6 +31,40 @@
return errInvalidType
}

if strings.TrimSpace(TagName) == "" {
return errInvalidTag

Check warning on line 35 in defaults.go

View check run for this annotation

Codecov / codecov/patch

defaults.go#L35

Added line #L35 was not covered by tests
}

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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: runtimeTag sounds a bit off to me. Reflection at its essence is about runtime.
tagName should suffice.

if reflect.TypeOf(ptr).Kind() != reflect.Ptr {
return errInvalidType

Check warning on line 61 in defaults.go

View check run for this annotation

Codecov / codecov/patch

defaults.go#L59-L61

Added lines #L59 - L61 were not covered by tests
}

if strings.TrimSpace(runtimeTag) == "" {
return errInvalidTag

Check warning on line 65 in defaults.go

View check run for this annotation

Codecov / codecov/patch

defaults.go#L64-L65

Added lines #L64 - L65 were not covered by tests
}

v := reflect.ValueOf(ptr).Elem()
t := v.Type()

Expand All @@ -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 != "-" {

Check warning on line 76 in defaults.go

View check run for this annotation

Codecov / codecov/patch

defaults.go#L76

Added line #L76 was not covered by tests
if err := setField(v.Field(i), defaultVal); err != nil {
return err
}
Expand Down