config: count.index parses
This commit is contained in:
parent
edbdad8fdb
commit
8756d52124
|
@ -52,6 +52,21 @@ type VariableInterpolation struct {
|
|||
Variable InterpolatedVariable
|
||||
}
|
||||
|
||||
// CountVariable is a variable for referencing information about
|
||||
// the count.
|
||||
type CountVariable struct {
|
||||
Type CountValueType
|
||||
key string
|
||||
}
|
||||
|
||||
// CountValueType is the type of the count variable that is referenced.
|
||||
type CountValueType byte
|
||||
|
||||
const (
|
||||
CountValueInvalid CountValueType = iota
|
||||
CountValueIndex
|
||||
)
|
||||
|
||||
// A ModuleVariable is a variable that is referencing the output
|
||||
// of a module, such as "${module.foo.bar}"
|
||||
type ModuleVariable struct {
|
||||
|
@ -84,7 +99,9 @@ type UserVariable struct {
|
|||
}
|
||||
|
||||
func NewInterpolatedVariable(v string) (InterpolatedVariable, error) {
|
||||
if strings.HasPrefix(v, "var.") {
|
||||
if strings.HasPrefix(v, "count.") {
|
||||
return NewCountVariable(v)
|
||||
} else if strings.HasPrefix(v, "var.") {
|
||||
return NewUserVariable(v)
|
||||
} else if strings.HasPrefix(v, "module.") {
|
||||
return NewModuleVariable(v)
|
||||
|
@ -152,6 +169,24 @@ func (i *VariableInterpolation) Variables() map[string]InterpolatedVariable {
|
|||
return map[string]InterpolatedVariable{i.Variable.FullKey(): i.Variable}
|
||||
}
|
||||
|
||||
func NewCountVariable(key string) (*CountVariable, error) {
|
||||
var fieldType CountValueType
|
||||
parts := strings.SplitN(key, ".", 2)
|
||||
switch parts[1] {
|
||||
case "index":
|
||||
fieldType = CountValueIndex
|
||||
}
|
||||
|
||||
return &CountVariable{
|
||||
Type: fieldType,
|
||||
key: key,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *CountVariable) FullKey() string {
|
||||
return c.key
|
||||
}
|
||||
|
||||
func NewModuleVariable(key string) (*ModuleVariable, error) {
|
||||
parts := strings.SplitN(key, ".", 3)
|
||||
if len(parts) < 3 {
|
||||
|
|
|
@ -29,6 +29,22 @@ func TestNewInterpolatedVariable(t *testing.T) {
|
|||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"count.index",
|
||||
&CountVariable{
|
||||
Type: CountValueIndex,
|
||||
key: "count.index",
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"count.nope",
|
||||
&CountVariable{
|
||||
Type: CountValueInvalid,
|
||||
key: "count.nope",
|
||||
},
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range cases {
|
||||
|
|
Loading…
Reference in New Issue