Skip to content
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

Improving slice and map support #379

Open
renom opened this issue Sep 20, 2023 · 0 comments
Open

Improving slice and map support #379

renom opened this issue Sep 20, 2023 · 0 comments

Comments

@renom
Copy link

renom commented Sep 20, 2023

After rethinking of #355, I came to a conclusion that it'd be useful to have the following approach to work with slices and maps in Kong:

A slice or map of struct (e.g. []SomeStruct or map[string]SomeStruct) isn't ignored by Kong as well as it isn't ignored if the struct has a slice or map field:

type SomeStruct struct {
  List []NestedStruct
  Dict map[string]NestedStruct
}

type NestedStruct struct {
  Count int
  Text string
}

I.e. if we have:

var CLI struct {
  Command struct {
    Data1 []SomeStruct
    Data2 map[string]SomeStruct
  } `cmd:""`
}

Then we have the following flags (total 8):

Command.Data1.#.List.#.Count
Command.Data1.#.List.#.Text
Command.Data1.#.Dict.?.Count
Command.Data1.#.Dict.?.Text
Command.Data2.?.List.#.Count
Command.Data2.?.List.#.Text
Command.Data2.?.Dict.?.Count
Command.Data2.?.Dict.?.Text

Those flags should be displayed in the help.

Prefixes (prefix and envPrefix) should work the same way as for regular flags with one exception:
Symbols # and ? will be used for a slice index and map key respectively:

var CLI struct {
  Command struct {
    Data1 []SomeStruct `envprefix:"DATA1_#_"`
    Data2 map[string]SomeStruct `cmd:"" envprefix:"DATA2_?_"`
  } `cmd:"" envprefix:"COMMAND_"`
}

type SomeStruct struct {
  List []NestedStruct `envprefix:"LIST_#_"`
  Dict map[string]NestedStruct `envprefix:"DICT_?_"`
}

type NestedStruct struct {
  Count int `env:"COUNT"`
  Text string `env:"TEXT"`
}

An example of .env:

COMMAND_DATA1_0_LIST_0_COUNT=5
COMMAND_DATA1_0_LIST_0_TEXT="AAAAA"
COMMAND_DATA1_0_LIST_1_COUNT=3
COMMAND_DATA1_0_LIST_1_TEXT="AAA"
COMMAND_DATA1_0_DICT_MAPKEY_COUNT=1
COMMAND_DATA1_0_DICT_MAPKEY_TEXT="A"
COMMAND_DATA1_0_DICT_ANOTHERKEY_COUNT=4
COMMAND_DATA1_0_DICT_ANOTHERKEY_TEXT="AAAA"
COMMAND_DATA1_1_LIST_0_COUNT=7
COMMAND_DATA1_1_LIST_0_TEXT="AAAAAAA"
COMMAND_DATA1_1_DICT_MAPKEY_COUNT=1
COMMAND_DATA1_1_DICT_MAPKEY_TEXT="A"

The vars with the same slice indexes (map keys) will refer to the same slice (map) element. I.e. COMMAND_DATA1_0_LIST_0_COUNT=5 and COMMAND_DATA1_0_LIST_0_TEXT="AAAAA" will fill up the same NestedStruct.

If a slice was prefilled with a resolver, the data from .env is appended to the slice so that the prefilled data can't be overridden (the indexes are just unique identifiers). On the contrary, map elements can be overridden.

Fields of every element of a slice or map should be validated with the specified Kong rules (enum, required etc.).

WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant