mcfg: made SourceCLI and SourceEnv be pointers

This commit is contained in:
Brian Picciano 2019-04-04 10:57:37 -04:00
parent 7752b569c3
commit a596306d9e
8 changed files with 28 additions and 22 deletions

6
m/m.go
View File

@ -28,7 +28,7 @@ func ProcessContext() context.Context {
ctx := context.Background() ctx := context.Background()
// embed confuration source which should be used into the context. // embed confuration source which should be used into the context.
ctx = context.WithValue(ctx, cfgSrcKey(0), mcfg.Source(mcfg.SourceCLI{})) ctx = context.WithValue(ctx, cfgSrcKey(0), mcfg.Source(new(mcfg.SourceCLI)))
// set up log level handling // set up log level handling
logger := mlog.NewLogger() logger := mlog.NewLogger()
@ -57,8 +57,8 @@ func ServiceContext() context.Context {
// services expect to use many different configuration sources // services expect to use many different configuration sources
ctx = context.WithValue(ctx, cfgSrcKey(0), mcfg.Source(mcfg.Sources{ ctx = context.WithValue(ctx, cfgSrcKey(0), mcfg.Source(mcfg.Sources{
mcfg.SourceEnv{}, new(mcfg.SourceEnv),
mcfg.SourceCLI{}, new(mcfg.SourceCLI),
})) }))
// TODO set up the debug endpoint. // TODO set up the debug endpoint.

View File

@ -53,7 +53,7 @@ const (
) )
// Parse implements the method for the Source interface // Parse implements the method for the Source interface
func (cli SourceCLI) Parse(params []Param) ([]ParamValue, error) { func (cli *SourceCLI) Parse(params []Param) ([]ParamValue, error) {
args := cli.Args args := cli.Args
if cli.Args == nil { if cli.Args == nil {
args = os.Args[1:] args = os.Args[1:]
@ -137,7 +137,7 @@ func (cli SourceCLI) Parse(params []Param) ([]ParamValue, error) {
return pvs, nil return pvs, nil
} }
func (cli SourceCLI) cliParams(params []Param) (map[string]Param, error) { func (cli *SourceCLI) cliParams(params []Param) (map[string]Param, error) {
m := map[string]Param{} m := map[string]Param{}
for _, p := range params { for _, p := range params {
key := strings.Join(append(mctx.Path(p.Context), p.Name), cliKeyJoin) key := strings.Join(append(mctx.Path(p.Context), p.Name), cliKeyJoin)
@ -146,7 +146,7 @@ func (cli SourceCLI) cliParams(params []Param) (map[string]Param, error) {
return m, nil return m, nil
} }
func (cli SourceCLI) printHelp(w io.Writer, pM map[string]Param) { func (cli *SourceCLI) printHelp(w io.Writer, pM map[string]Param) {
type pEntry struct { type pEntry struct {
arg string arg string
Param Param

View File

@ -26,7 +26,7 @@ func TestSourceCLIHelp(t *T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
pM, err := src.cliParams(collectParams(ctx)) pM, err := src.cliParams(collectParams(ctx))
require.NoError(t, err) require.NoError(t, err)
SourceCLI{}.printHelp(buf, pM) new(SourceCLI).printHelp(buf, pM)
exp := ` exp := `
--baz2 (Required) --baz2 (Required)
@ -49,7 +49,7 @@ func TestSourceCLIHelp(t *T) {
func TestSourceCLI(t *T) { func TestSourceCLI(t *T) {
type state struct { type state struct {
srcCommonState srcCommonState
SourceCLI *SourceCLI
} }
type params struct { type params struct {
@ -61,7 +61,9 @@ func TestSourceCLI(t *T) {
Init: func() mchk.State { Init: func() mchk.State {
var s state var s state
s.srcCommonState = newSrcCommonState() s.srcCommonState = newSrcCommonState()
s.SourceCLI.Args = make([]string, 0, 16) s.SourceCLI = &SourceCLI{
Args: make([]string, 0, 16),
}
return s return s
}, },
Next: func(ss mchk.State) mchk.Action { Next: func(ss mchk.State) mchk.Action {
@ -111,9 +113,11 @@ func TestSourceCLITailCallback(t *T) {
ctx, _ = WithBool(ctx, "bar", "") ctx, _ = WithBool(ctx, "bar", "")
var tail []string var tail []string
src := SourceCLI{TailCallback: func(gotTail []string) { src := &SourceCLI{
tail = gotTail TailCallback: func(gotTail []string) {
}} tail = gotTail
},
}
type testCase struct { type testCase struct {
args []string args []string

View File

@ -32,7 +32,7 @@ type SourceEnv struct {
Prefix string Prefix string
} }
func (env SourceEnv) expectedName(path []string, name string) string { func (env *SourceEnv) expectedName(path []string, name string) string {
out := strings.Join(append(path, name), "_") out := strings.Join(append(path, name), "_")
if env.Prefix != "" { if env.Prefix != "" {
out = env.Prefix + "_" + out out = env.Prefix + "_" + out
@ -43,7 +43,7 @@ func (env SourceEnv) expectedName(path []string, name string) string {
} }
// Parse implements the method for the Source interface // Parse implements the method for the Source interface
func (env SourceEnv) Parse(params []Param) ([]ParamValue, error) { func (env *SourceEnv) Parse(params []Param) ([]ParamValue, error) {
kvs := env.Env kvs := env.Env
if kvs == nil { if kvs == nil {
kvs = os.Environ() kvs = os.Environ()

View File

@ -11,7 +11,7 @@ import (
func TestSourceEnv(t *T) { func TestSourceEnv(t *T) {
type state struct { type state struct {
srcCommonState srcCommonState
SourceEnv *SourceEnv
} }
type params struct { type params struct {
@ -22,7 +22,9 @@ func TestSourceEnv(t *T) {
Init: func() mchk.State { Init: func() mchk.State {
var s state var s state
s.srcCommonState = newSrcCommonState() s.srcCommonState = newSrcCommonState()
s.SourceEnv.Env = make([]string, 0, 16) s.SourceEnv = &SourceEnv{
Env: make([]string, 0, 16),
}
return s return s
}, },
Next: func(ss mchk.State) mchk.Action { Next: func(ss mchk.State) mchk.Action {

View File

@ -17,7 +17,7 @@ func TestPopulate(t *T) {
ctxChild, c := WithInt(ctxChild, "c", 0, "") ctxChild, c := WithInt(ctxChild, "c", 0, "")
ctx = mctx.WithChild(ctx, ctxChild) ctx = mctx.WithChild(ctx, ctxChild)
err := Populate(ctx, SourceCLI{ err := Populate(ctx, &SourceCLI{
Args: []string{"--a=1", "--foo-b=2"}, Args: []string{"--a=1", "--foo-b=2"},
}) })
assert.NoError(t, err) assert.NoError(t, err)
@ -34,12 +34,12 @@ func TestPopulate(t *T) {
ctxChild, c := WithRequiredInt(ctxChild, "c", "") ctxChild, c := WithRequiredInt(ctxChild, "c", "")
ctx = mctx.WithChild(ctx, ctxChild) ctx = mctx.WithChild(ctx, ctxChild)
err := Populate(ctx, SourceCLI{ err := Populate(ctx, &SourceCLI{
Args: []string{"--a=1", "--foo-b=2"}, Args: []string{"--a=1", "--foo-b=2"},
}) })
assert.Error(t, err) assert.Error(t, err)
err = Populate(ctx, SourceCLI{ err = Populate(ctx, &SourceCLI{
Args: []string{"--a=1", "--foo-b=2", "--foo-c=3"}, Args: []string{"--a=1", "--foo-b=2", "--foo-c=3"},
}) })
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -160,8 +160,8 @@ func TestSources(t *T) {
ctx, c := WithRequiredInt(ctx, "c", "") ctx, c := WithRequiredInt(ctx, "c", "")
err := Populate(ctx, Sources{ err := Populate(ctx, Sources{
SourceCLI{Args: []string{"--a=1", "--b=666"}}, &SourceCLI{Args: []string{"--a=1", "--b=666"}},
SourceEnv{Env: []string{"B=2", "C=3"}}, &SourceEnv{Env: []string{"B=2", "C=3"}},
}) })
massert.Require(t, massert.Require(t,
massert.Nil(err), massert.Nil(err),

View File

@ -50,7 +50,7 @@ func Run(ctx context.Context, t *testing.T, body func()) {
env = append(env, tup[0]+"="+tup[1]) env = append(env, tup[0]+"="+tup[1])
} }
if err := mcfg.Populate(ctx, mcfg.SourceEnv{Env: env}); err != nil { if err := mcfg.Populate(ctx, &mcfg.SourceEnv{Env: env}); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := mrun.Start(ctx); err != nil { } else if err := mrun.Start(ctx); err != nil {
t.Fatal(err) t.Fatal(err)