Skip to content

Commit

Permalink
Refactoring decode function
Browse files Browse the repository at this point in the history
  • Loading branch information
greencoda committed Apr 24, 2024
1 parent d52b86f commit 14d32e6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 36 deletions.
14 changes: 0 additions & 14 deletions configSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,6 @@ func New(options ...configSetOption) *ConfigSet {
return configSet
}

// Decode decodes the configuration values into the target struct.
func (c *ConfigSet) Decode(target interface{}, options ...decodeOption) error {
decodeOptions := &decodeSettings{
strict: false,
prefix: "",
}

for _, option := range options {
option(decodeOptions)
}

return c.decode(target, decodeOptions.prefix, decodeOptions.strict)
}

// Get returns the configuration value at the given path as an interface.
func (c *ConfigSet) Get(path string) (any, error) {
return c.getByPath(path)
Expand Down
50 changes: 28 additions & 22 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,25 @@ type fieldOptions struct {
defaultValue *string
}

func newEmptyFieldOptions() fieldOptions {
return fieldOptions{
path: "",
strict: false,
required: false,
defaultValue: nil,
}
}

type Decoder interface {
Decode(value any) error
}

func (c *ConfigSet) decode(target interface{}, path string, strict bool) error {
// Decode decodes the configuration values into the target struct.
func (c *ConfigSet) Decode(target interface{}, options ...decodeOption) error {
return c.decode(target, options)
}

func (c *ConfigSet) decode(target interface{}, options []decodeOption) error {
decodeSettings := &decodeSettings{
strict: false,
prefix: "",
}

for _, option := range options {
option(decodeSettings)
}

targetValue := reflect.ValueOf(target)
if targetValue.Kind() != reflect.Ptr || targetValue.IsNil() {
return ErrInvalidTarget
Expand All @@ -59,8 +64,8 @@ func (c *ConfigSet) decode(target interface{}, path string, strict bool) error {
targetValue = targetValue.Elem()

if decodedFieldCount, err := c.decodeField(targetValue, fieldOptions{
path: path,
strict: strict,
path: decodeSettings.prefix,
strict: decodeSettings.strict,
required: false,
defaultValue: nil,
}); err != nil {
Expand Down Expand Up @@ -330,20 +335,21 @@ func (c *ConfigSet) decodePrimitiveType(primitiveValue reflect.Value, configValu
}

func (c *ConfigSet) readTag(field reflect.StructField, tag string) fieldOptions {
fieldOpts := fieldOptions{
path: "",
strict: false,
required: false,
defaultValue: nil,
}

tagValue := field.Tag.Get(tag)
if tagValue == "" {
return newEmptyFieldOptions()
return fieldOpts
}

var (
tagParts = strings.Split(tagValue, ",")
fieldOpts = fieldOptions{
path: tagParts[0],
strict: false,
required: false,
defaultValue: nil,
}
)
tagParts := strings.Split(tagValue, ",")

fieldOpts.path = tagParts[0]

// read the remaining tag parts
for _, part := range tagParts[1:] {
Expand Down

0 comments on commit 14d32e6

Please sign in to comment.