-
Notifications
You must be signed in to change notification settings - Fork 51
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
Add simplemap backend #36
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package simplemap | ||
|
||
import ( | ||
"github.com/heetch/confita" | ||
"github.com/pkg/errors" | ||
"reflect" | ||
|
||
"context" | ||
) | ||
|
||
// Backend that loads config from map | ||
type Backend struct { | ||
theMap map[string]interface{} | ||
} | ||
|
||
// NewBackend creates a simplemap backend. | ||
func NewBackend(theMap map[string]interface{}) *Backend { | ||
return &Backend{ | ||
theMap: theMap, | ||
} | ||
} | ||
|
||
// LoadStruct takes a struct config and loads the map into it | ||
func (b *Backend) LoadStruct(ctx context.Context, cfg *confita.StructConfig) error { | ||
for _, f := range cfg.Fields { | ||
mapVal := b.theMap[f.Key] | ||
if mapVal == nil { | ||
continue | ||
} | ||
mapRef := reflect.ValueOf(mapVal) | ||
f.Value.Set(mapRef) | ||
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. This will panic if the Perhaps it would be easiest to implement the Backend on |
||
} | ||
return nil | ||
} | ||
|
||
// Get is not implemented. | ||
func (b *Backend) Get(ctx context.Context, key string) ([]byte, error) { | ||
return nil, errors.New("not implemented") | ||
} | ||
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. I think that this is redundant with what we do in the convert function. Moreover you are not handling all the primitive and this is very problematic, for example if the type is an 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. Thank you! Tried the StructLoader way, take a look :) |
||
|
||
// Name returns the name of the flags backend. | ||
func (b *Backend) Name() string { | ||
return "simplemap" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package simplemap_test | ||
|
||
import ( | ||
"context" | ||
"github.com/heetch/confita" | ||
"github.com/heetch/confita/backend/simplemap" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSimplemapBackend(t *testing.T) { | ||
type config struct { | ||
Name string `config:"Name"` | ||
Age int `config:"Age"` | ||
Negative int `config:"Negative"` | ||
Balance float64 `config:"Balance"` | ||
Timeout time.Duration `config:"Timeout"` | ||
} | ||
|
||
b := simplemap.NewBackend(map[string]interface{}{ | ||
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. Let's have some test cases that test error cases, such as mismatched types, please. |
||
"Name": "Bob", | ||
"Age": 30, | ||
"Negative": -10, | ||
"Balance": 1234.56, | ||
"Timeout": 10 * time.Second, | ||
}) | ||
|
||
var c config | ||
require.Equal(t, "simplemap", b.Name()) | ||
err := confita.NewLoader(b).Load(context.Background(), &c) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "Bob", c.Name) | ||
require.Equal(t, 30, c.Age) | ||
require.Equal(t, -10, c.Negative) | ||
require.Equal(t, 1234.56, c.Balance) | ||
require.Equal(t, 10*time.Second, c.Timeout) | ||
} |
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.
How about just:
?
Then we wouldn't need the
NewBackend
constructor function.