From 7310ce2c1120c4e5df08f8f38e9ff2edf33bebe7 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Wed, 5 Oct 2022 21:21:15 +0200 Subject: [PATCH] fix Settings should be exported --- api.go | 29 ++++------------------------- command.go | 4 ++-- command_test.go | 2 +- error.go | 6 +++--- execute.go | 2 +- execute_test.go | 6 +++--- flag.go | 2 +- integration_test.go | 2 +- parse.go | 2 +- setting.go | 6 +++--- setting_test.go | 4 ++-- shadow.go | 2 +- 12 files changed, 23 insertions(+), 44 deletions(-) diff --git a/api.go b/api.go index 50dbaf0..ddc7e43 100644 --- a/api.go +++ b/api.go @@ -10,9 +10,9 @@ import ( // New creates a new instance of the settings. // name should be the name of the command and comes from the first argument of the command line. // os.Args[0] is a good choice. -func New[C any](name string, definitions C) *setting[C] { +func New[C any](name string, definitions C) *Settings[C] { - s := &setting[C]{ + s := &Settings[C]{ config: config{ errorHandling: flag.ContinueOnError, }, @@ -32,32 +32,11 @@ func New[C any](name string, definitions C) *setting[C] { } // FlagOutput returns the writer where the flag package writes its output. -func (s *setting[C]) FlagOutput() string { +func (s *Settings[C]) FlagOutput() string { return s.flagOutput.(*bytes.Buffer).String() } // Args Returns not parsed arguments. -func (s *setting[C]) Args() []string { +func (s *Settings[C]) Args() []string { return s.args } - -//type ConfigAny[C any] struct { -// path string -// ConfigurationAdapter[C] -//} -// -//type ConfigurationAdapter[RT any] interface { -// InitFromFlagSet(flagSet *flag.FlagSet) RT -//} - -//func (s *setting[C]) CopyValuesToStruct(path string, adapter any) *setting[C] { -// -// if s.command == nil { -// s.errors = append(s.errors, MissingCommandError) -// return s -// } -// -// s.command.copyValuesToStruct(path, adapter) -// -// return s -//} diff --git a/command.go b/command.go index 278c038..cbfcbe7 100644 --- a/command.go +++ b/command.go @@ -11,7 +11,7 @@ type cmd[C any] struct { tagMapping map[string]string shadowMapping map[string]string commands []*cmd[C] - settings *setting[C] + settings *Settings[C] valuePath []string functionName string } @@ -51,7 +51,7 @@ func (c *cmd[C]) parse(args []string) { } -func buildCommandStruct[C any](s *setting[C], name, fkt string, errorHandling flag.ErrorHandling, path []string) *cmd[C] { +func buildCommandStruct[C any](s *Settings[C], name, fkt string, errorHandling flag.ErrorHandling, path []string) *cmd[C] { cc := &cmd[C]{ name: name, flagSet: flag.NewFlagSet(name, errorHandling), diff --git a/command_test.go b/command_test.go index 8ecdf9c..e44467c 100644 --- a/command_test.go +++ b/command_test.go @@ -30,7 +30,7 @@ func TestParse(t *testing.T) { t.Run(test.commandline[0], func(t *testing.T) { c := &cmd[testStructParseCommand1]{} - c.settings = &setting[testStructParseCommand1]{} + c.settings = &Settings[testStructParseCommand1]{} c.commands = []*cmd[testStructParseCommand1]{} diff --git a/error.go b/error.go index a31a283..be4a46f 100644 --- a/error.go +++ b/error.go @@ -7,18 +7,18 @@ import ( // ResetError is used to reset the error to nil // After calling this function, the call HasErrors() will return false -func (s *setting[C]) ResetErrors() *setting[C] { +func (s *Settings[C]) ResetErrors() *Settings[C] { s.errors = []error{} return s } // Check if the setting contains errors -func (s *setting[C]) HasErrors() bool { +func (s *Settings[C]) HasErrors() bool { return len(s.errors) > 0 } // Get all errors -func (s *setting[C]) Errors() []error { +func (s *Settings[C]) Errors() []error { return s.errors } diff --git a/execute.go b/execute.go index 1878761..d39e3c7 100644 --- a/execute.go +++ b/execute.go @@ -4,7 +4,7 @@ import ( "reflect" ) -func (s *setting[C]) Execute() *setting[C] { +func (s *Settings[C]) Execute() *Settings[C] { if len(s.errors) > 0 { return s } diff --git a/execute_test.go b/execute_test.go index 2625eb7..43335b8 100644 --- a/execute_test.go +++ b/execute_test.go @@ -25,15 +25,15 @@ type testExecutionStruct struct { } `command:"command3" description:"Command 3" call:"Command3Callback" ` } -func (c *testExecutionStruct) Command1Callback(s *setting[testExecutionStruct]) { +func (c *testExecutionStruct) Command1Callback(s *Settings[testExecutionStruct]) { s.definitions.callbackCounter++ } -func (c *testExecutionStruct) Command2Callback(s *setting[testExecutionStruct]) { +func (c *testExecutionStruct) Command2Callback(s *Settings[testExecutionStruct]) { s.definitions.callbackCounter++ } -func (c *testExecutionStruct) Command3Callback(s *setting[testExecutionStruct]) { +func (c *testExecutionStruct) Command3Callback(s *Settings[testExecutionStruct]) { s.definitions.callbackCounter++ } diff --git a/flag.go b/flag.go index eb9f08c..2f2d8b4 100644 --- a/flag.go +++ b/flag.go @@ -5,7 +5,7 @@ import ( "strings" ) -func (s *setting[C]) assignValues(c cmd[C]) { +func (s *Settings[C]) assignValues(c cmd[C]) { flgs := c.flagSet flgs.Visit(func(f *flag.Flag) { diff --git a/integration_test.go b/integration_test.go index b7ff83e..3b076d9 100644 --- a/integration_test.go +++ b/integration_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -type testInterfaceCallbacks func(s *setting[testIntegrationStruct]) +type testInterfaceCallbacks func(s *Settings[testIntegrationStruct]) type testIntegrationStruct struct { Help bool `short:"h" long:"help" description:"Show this help message"` diff --git a/parse.go b/parse.go index 24c0e26..729f091 100644 --- a/parse.go +++ b/parse.go @@ -8,7 +8,7 @@ func (s *Settings[C]) ParseOsArgs() *Settings[C] { } // Parse parses the command line arguments and assigns the values to the settings. -func (s *setting[C]) Parse(args []string) *setting[C] { +func (s *Settings[C]) Parse(args []string) *Settings[C] { if len(s.errors) > 0 { return s } diff --git a/setting.go b/setting.go index 4a73c4b..a87a954 100644 --- a/setting.go +++ b/setting.go @@ -5,7 +5,7 @@ import ( "io" ) -func (s *setting[C]) initCommands(name string) { +func (s *Settings[C]) initCommands(name string) { s.command = buildCommandStruct[C](s, name, "", s.config.errorHandling, []string{}) s.command.parseStruct(s.definitions) } @@ -14,7 +14,7 @@ type config struct { errorHandling flag.ErrorHandling } -type setting[C any] struct { +type Settings[C any] struct { definitions C command *cmd[C] @@ -29,6 +29,6 @@ type setting[C any] struct { shadow any } -func (s *setting[C]) GetValues() C { +func (s *Settings[C]) GetValues() C { return s.definitions } diff --git a/setting_test.go b/setting_test.go index 23b1265..9e7c7a5 100644 --- a/setting_test.go +++ b/setting_test.go @@ -6,14 +6,14 @@ import ( ) func TestGetValues(t *testing.T) { - s := &setting[CmdTest1]{ + s := &Settings[CmdTest1]{ definitions: CmdTest1{}, } assert.Equal(t, CmdTest1{}, s.GetValues()) } func TestInitCommands(t *testing.T) { - s := &setting[CmdTest1]{ + s := &Settings[CmdTest1]{ definitions: CmdTest1{}, } diff --git a/shadow.go b/shadow.go index b41717f..2aae3a8 100644 --- a/shadow.go +++ b/shadow.go @@ -3,7 +3,7 @@ package xflags import "reflect" // SetShadow sets the shadow struct for the flag configuration. -func (s *setting[C]) SetShadow(shadow any) *setting[C] { +func (s *Settings[C]) SetShadow(shadow any) *Settings[C] { if reflect.TypeOf(shadow).Kind() != reflect.Ptr { s.errors = append(s.errors, ShadowMustBePointerError) -- GitLab