From 3fad5a690e69d25c833c34f3890c331a34919ee8 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Sun, 23 Oct 2022 22:15:19 +0200 Subject: [PATCH] feat change proxy to map and expose map --- README.md | 40 ++++++++++++++++++++++------------------ api.go | 10 +++++++--- api_test.go | 18 ++++++++++++++++++ command.go | 4 ++-- execute_test.go | 4 ++-- mapping.go | 8 ++++---- mapping_test.go | 2 +- setting.go | 2 +- tags.go | 2 +- 9 files changed, 58 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 8cc9af2..bb8c233 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,7 @@ It supports: * [X] Define flags in a structure * [X] Define callbacks for flags * [X] Define default values for flags -* [X] Define aliases for flags -* [X] Define required flags +* [X] Define a map for values ## Installation @@ -57,16 +56,16 @@ type Definition struct { The following tags are supported: -| Tag | Context | Description | -|---------------|----------|------------------------------------------------| -| `short` | Value | Short name of the flag. | -| `long` | Value | Long name of the flag. | -| `description` | Value | Description of the flag. | -| `required` | Value | Flag is required. | -| `proxy` | Value | Copy the value to the proxy structure. | -| `command` | Command | Flag is a command. | -| `call` | Command | Function to call when the command is used. | -| `ignore` | -/- | Property is ignored. | +| Tag | Context | Description | +|---------------|----------|--------------------------------------------| +| `short` | Value | Short name of the flag. | +| `long` | Value | Long name of the flag. | +| `description` | Value | Description of the flag. | +| `required` | Value | Flag is required. | +| `map` | Value | Copy the value to the mapped structure. | +| `command` | Command | Flag is a command. | +| `call` | Command | Function to call when the command is used. | +| `ignore` | -/- | Property is ignored. | ### Callbacks @@ -147,12 +146,13 @@ The function `Execute()` executes the command. See the section setting.Execute() ``` -### Proxy +### Mapped Values -The proxy structure is used to copy the values of the flags to a other -structure. +The mapped structure is used to copy the +values of the flags to another structure +and to a map. -The proxy structure must implement the `Proxy` interface. +The mapped structure must implement the `Copyable` interface. ```go type MyObj struct { @@ -168,13 +168,17 @@ func (m *MyObj) Copy(_ map[string]any) { func main() { setting := New(os.Args[0], Definition{}) - setting.SetProxy(&MyObj{}) + setting.SetMappedObject(&MyObj{}) setting.Parse(os.Args[1:]) setting.Execute() } ``` -The path in the structure is defined by the tag `proxy`. +The path in the structure is defined by the tag `map`. + +Die Map der Werte kann über die Methode `GetMap()` abgerufen werden. + + ### Arguments diff --git a/api.go b/api.go index b6fc6ac..efdde26 100644 --- a/api.go +++ b/api.go @@ -17,7 +17,7 @@ type dummyCopyArg struct{} func (n dummyCopyArg) Copy(_ map[string]any) {} // Execute executes the command line arguments and calls the functions. -func Execute[C any](cmd C, cpy ...Proxy) *Settings[C] { +func Execute[C any](cmd C, cpy ...Copyable) *Settings[C] { if cpy == nil { return execute(cmd, dummyCopyArg{}, os.Args[0], os.Args[1:]) @@ -41,14 +41,14 @@ func (s *Settings[C]) GetFlagOutput() { } // execute is the internal implementation of Execute. -func execute[C any, D Proxy](cmd C, proxy D, name string, args []string) *Settings[C] { +func execute[C any, D Copyable](cmd C, proxy D, name string, args []string) *Settings[C] { instance := New(name, cmd) if instance.HasErrors() { return instance } if (reflect.ValueOf(&proxy).Elem().Type() != reflect.TypeOf(dummyCopyArg{})) { - instance.SetProxy(proxy) + instance.SetMappedObject(proxy) if instance.HasErrors() { return instance } @@ -116,3 +116,7 @@ func (s *Settings[C]) GetDefaults() string { s.flagOutput = mem return r } + +func (s *Settings[C]) GetMap() map[string]any { + return s.mapping +} diff --git a/api_test.go b/api_test.go index fa7e925..8bec109 100644 --- a/api_test.go +++ b/api_test.go @@ -102,3 +102,21 @@ func TestCommand2(t *testing.T) { assert.False(t, commands.HasErrors()) } + +func TestProxyMapping(t *testing.T) { + + commands := New("root", testExecutionStruct{}) + args := []string{"-a", "command1", "-d"} + + commands.Parse(args) + + assert.False(t, commands.HasErrors()) + if commands.HasErrors() { + t.Log(commands.Errors()) + } + + m := commands.GetMap() + assert.Equal(t, "true", m["ValGlobal1"]) + assert.Equal(t, "true", m["ValCommand1Flag2"]) + +} diff --git a/command.go b/command.go index eccb273..c5b4554 100644 --- a/command.go +++ b/command.go @@ -150,8 +150,8 @@ func (c *cmd[C]) parseStruct(dta any) { continue } - if m[tagProxy] != "" { - c.proxyMapping[v.Type().Field(i).Name] = m[tagProxy] + if m[tagMapping] != "" { + c.proxyMapping[v.Type().Field(i).Name] = m[tagMapping] } if m[tagShort] != "" { diff --git a/execute_test.go b/execute_test.go index bb19486..5275ca7 100644 --- a/execute_test.go +++ b/execute_test.go @@ -13,11 +13,11 @@ type testExecutionStruct struct { callbackCounter int `ignore:"true"` // for tag proxy see TestFlagCopyToProxy - Global1 bool `short:"a" long:"global1" description:"Global 1" proxy:"ValGlobal1"` + Global1 bool `short:"a" long:"global1" description:"Global 1" map:"ValGlobal1"` Global2 bool `short:"b" long:"global2" description:"Global 2"` Command1 struct { Command1Flag1 bool `short:"c" long:"command1flag1" description:"Command 1 Flag 1"` - Command1Flag2 bool `short:"d" long:"command1flag2" description:"Command 1 Flag 2" proxy:"ValCommand1Flag2"` + Command1Flag2 bool `short:"d" long:"command1flag2" description:"Command 1 Flag 2" map:"ValCommand1Flag2"` Command2 struct { Command2Flag1 bool `short:"e" long:"command2flag1" description:"Command 2 Flag 1"` Command2Flag2 bool `short:"f" long:"command2flag2" description:"Command 2 Flag 2"` diff --git a/mapping.go b/mapping.go index dd1e3de..7531bb5 100644 --- a/mapping.go +++ b/mapping.go @@ -10,8 +10,8 @@ import ( "strings" ) -// SetProxy sets the shadow struct for the flag configuration. -func (s *Settings[C]) SetProxy(proxy Proxy) *Settings[C] { +// SetMappedObject sets the shadow struct for the flag configuration. +func (s *Settings[C]) SetMappedObject(proxy Copyable) *Settings[C] { if reflect.TypeOf(proxy).Kind() != reflect.Ptr { s.errors = append(s.errors, ShadowMustBePointerError) @@ -27,8 +27,8 @@ func (s *Settings[C]) SetProxy(proxy Proxy) *Settings[C] { return s } -// Proxy is the interface for the proxy struct. -type Proxy interface { +// Copyable is the interface for the proxy struct. +type Copyable interface { Copy(map[string]any) } diff --git a/mapping_test.go b/mapping_test.go index d7698c1..46a712a 100644 --- a/mapping_test.go +++ b/mapping_test.go @@ -29,7 +29,7 @@ func TestFlagCopyToProxy(t *testing.T) { settings := New("test", testExecutionStruct{}) assert.NotNil(t, settings) - settings.SetProxy(&c) + settings.SetMappedObject(&c) assert.False(t, settings.HasErrors()) settings.Parse([]string{"-a", "command1", "-d"}) diff --git a/setting.go b/setting.go index f4456a3..78e1acd 100644 --- a/setting.go +++ b/setting.go @@ -31,7 +31,7 @@ type Settings[C any] struct { config config mapping map[string]any - proxy Proxy + proxy Copyable wasExecuted bool hint string diff --git a/tags.go b/tags.go index 47fa538..f019823 100644 --- a/tags.go +++ b/tags.go @@ -15,7 +15,7 @@ const ( tagShort = "short" tagLong = "long" tagDescription = "description" - tagProxy = "proxy" + tagMapping = "map" ) func getTagMap(field reflect.StructField) (value map[string]string) { -- GitLab