MCPcopy Create free account
hub / github.com/foxcpp/maddy / Enum

Method Enum

framework/config/map.go:117–136  ·  view source on GitHub ↗

Enum maps a configuration directive to a string variable. Directive must be in form 'name string' where string should be from *allowed* slice. That string argument will be stored in store variable. See Map.Custom for description of inheritGlobal and required.

(name string, inheritGlobal, required bool, allowed []string, defaultVal string, store *string)

Source from the content-addressed store, hash-verified

115//
116// See Map.Custom for description of inheritGlobal and required.
117func (m *Map) Enum(name string, inheritGlobal, required bool, allowed []string, defaultVal string, store *string) {
118 m.Custom(name, inheritGlobal, required, func() (interface{}, error) {
119 return defaultVal, nil
120 }, func(_ *Map, node Node) (interface{}, error) {
121 if len(node.Children) != 0 {
122 return nil, NodeErr(node, "can't declare a block here")
123 }
124 if len(node.Args) != 1 {
125 return nil, NodeErr(node, "expected exactly one argument")
126 }
127
128 for _, str := range allowed {
129 if str == node.Args[0] {
130 return node.Args[0], nil
131 }
132 }
133
134 return nil, NodeErr(node, "invalid argument, valid values are: %v", allowed)
135 }, store)
136}
137
138// EnumMapped is similar to Map.Enum but maps a stirng to a custom type.
139func EnumMapped[V any](m *Map, name string, inheritGlobal, required bool, mapped map[string]V, defaultVal V, store *V) {

Callers 3

ConfigureMethod · 0.80
ConfigureMethod · 0.80
ConfigureMethod · 0.80

Calls 2

CustomMethod · 0.95
NodeErrFunction · 0.70

Tested by

no test coverage detected