Skip to content

Commit

Permalink
Migrate tests to testify (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
efritz authored Oct 1, 2020
1 parent 92206e8 commit 4b3e2ca
Show file tree
Hide file tree
Showing 51 changed files with 959 additions and 1,134 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2019 Eric Fritz
Copyright (c) 2020 Eric Fritz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
92 changes: 45 additions & 47 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,47 @@ import (
"strings"
)

type (
// Config is a structure that can populate the exported fields of a
// struct based on the value of the field `env` tags.
Config interface {
// Init prepares state required by the registered sourcer. This
// method should be called before calling any other method.
Init() error

// Load populates a configuration object. The given tag modifiers
// are applied to the configuration object pre-load. If the target
// value conforms to the PostLoadConfig interface, the PostLoad
// function may be called multiple times.
Load(interface{}, ...TagModifier) error

// MustInject calls Injects and panics on error.
MustLoad(interface{}, ...TagModifier)

// Assets returns a list of names of assets that compose the
// underlying sourcer. This can be a list of matched files that are
// read, or a token that denotes a fixed source.
Assets() []string

// Dump returns the full content of the underlying sourcer. This
// is used by the logging package to show the content of the
// environment and config files when a value is missing or otherwise
// illegal.
Dump() map[string]string
}

// PostLoadConfig is a marker interface for configuration objects
// which should do some post-processing after being loaded. This
// can perform additional casting (e.g. ints to time.Duration) and
// more sophisticated validation (e.g. enum or exclusive values).
PostLoadConfig interface {
PostLoad() error
}

config struct {
sourcer Sourcer
}
)
// Config is a structure that can populate the exported fields of a
// struct based on the value of the field `env` tags.
type Config interface {
// Init prepares state required by the registered sourcer. This
// method should be called before calling any other method.
Init() error

// Load populates a configuration object. The given tag modifiers
// are applied to the configuration object pre-load. If the target
// value conforms to the PostLoadConfig interface, the PostLoad
// function may be called multiple times.
Load(interface{}, ...TagModifier) error

// MustInject calls Injects and panics on error.
MustLoad(interface{}, ...TagModifier)

// Assets returns a list of names of assets that compose the
// underlying sourcer. This can be a list of matched files that are
// read, or a token that denotes a fixed source.
Assets() []string

// Dump returns the full content of the underlying sourcer. This
// is used by the logging package to show the content of the
// environment and config files when a value is missing or otherwise
// illegal.
Dump() map[string]string
}

// PostLoadConfig is a marker interface for configuration objects
// which should do some post-processing after being loaded. This
// can perform additional casting (e.g. ints to time.Duration) and
// more sophisticated validation (e.g. enum or exclusive values).
type PostLoadConfig interface {
PostLoad() error
}

type config struct {
sourcer Sourcer
}

var _ Config = &config{}

// NewConfig creates a config loader with the given sourcer.
func NewConfig(sourcer Sourcer) Config {
Expand Down Expand Up @@ -119,12 +119,10 @@ func (c *config) loadStruct(objValue reflect.Value, objType reflect.Type) []erro

errors := []error{}
for i := 0; i < objType.NumField(); i++ {
var (
field = objValue.Field(i)
fieldType = objType.Field(i)
defaultTagValue = fieldType.Tag.Get(DefaultTag)
requiredTagValue = fieldType.Tag.Get(RequiredTag)
)
field := objValue.Field(i)
fieldType := objType.Field(i)
defaultTagValue := fieldType.Tag.Get(DefaultTag)
requiredTagValue := fieldType.Tag.Get(RequiredTag)

if fieldType.Anonymous {
errors = append(errors, c.loadStruct(field, fieldType.Type)...)
Expand Down
18 changes: 7 additions & 11 deletions config_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4b3e2ca

Please sign in to comment.