Skip to content

Array variables PoC #748

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions arraytemplate/array_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2020 The Compose Specification Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package arraytemplate

import (
"fmt"
"regexp"
)

type Mapping func(string) (string, bool)

const (
maxArraySize = 100

substitution = "[_a-zA-Z][_a-zA-Z0-9]*"
)

var (
rawPattern = fmt.Sprintf(
"^\\$(?:(%s)\\[\\*]|{(%s)\\[\\*]})$",
substitution,
substitution,
)

ArraySubstitutionPattern = regexp.MustCompile(rawPattern)
)

func Substitute(template string, mapping Mapping) ([]string, error) {
arrayName, err := findArrayName(template)
if err != nil {
return nil, err
}
inlined, err := getInlined(arrayName, mapping)
if err != nil {
return nil, fmt.Errorf("could not substitute array template \"%s\":\n%w", template, err)
}

if inlined != nil {
return inlined, nil
}

return getIndexed(arrayName, mapping), nil
}

func findArrayName(template string) (string, error) {
matches := ArraySubstitutionPattern.FindStringSubmatch(template)

if len(matches) < 1 {
return "", fmt.Errorf("not a valid array template: \"%s\"", template)
}

var arrayName string
for _, match := range matches[1:] {
if match != "" {
arrayName = match
break
}
}

if arrayName == "" {
return "", fmt.Errorf("this message suggest an internal error and should never occur; if you see this error, please report it: \"%s\"", template)
}

return arrayName, nil
}
276 changes: 276 additions & 0 deletions arraytemplate/array_template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
/*
Copyright 2020 The Compose Specification Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package arraytemplate

import (
"fmt"
"gotest.tools/v3/assert"
"testing"
)

func buildMapping(mapping map[string]string) func(string) (string, bool) {
return func(name string) (string, bool) {
v, ok := mapping[name]
return v, ok
}
}

func TestIndexedArraysSubstitutions(t *testing.T) {
testcases := []struct {
template string
mapping map[string]string
expectedArray []string
}{
{
template: "$INDEXED_ARRAY[*]",
mapping: map[string]string{
"INDEXED_ARRAY[0]": "zero",
"INDEXED_ARRAY[1]": "one",
"INDEXED_ARRAY[2]": "two",
},
expectedArray: []string{"zero", "one", "two"},
},
{
template: "${BRACED_INDEXED_ARRAY[*]}",
mapping: map[string]string{
"BRACED_INDEXED_ARRAY[0]": "zero",
"BRACED_INDEXED_ARRAY[1]": "one",
"BRACED_INDEXED_ARRAY[2]": "two",
},
expectedArray: []string{"zero", "one", "two"},
},
{
template: "$EMPTY_ARRAY[*]",
mapping: map[string]string{
"NON_RELEVANT": "WHATEVER",
},
expectedArray: []string{},
},
{
template: "$MISINDEXED_ARRAY[*]",
mapping: map[string]string{
"MISINDEXED_ARRAY[1]": "zero",
"MISINDEXED_ARRAY[2]": "one",
"MISINDEXED_ARRAY[3]": "two",
},
expectedArray: []string{},
},
{
template: "$INTERRUPTED_ARRAY[*]",
mapping: map[string]string{
"INTERRUPTED_ARRAY[0]": "zero",
"INTERRUPTED_ARRAY[1": "one",
"INTERRUPTED_ARRAY[2]": "two",
},
expectedArray: []string{"zero"},
},
{
template: "$INVALID_KEY[*]",
mapping: map[string]string{
"INDEXED_ARRAY[0]": "zero",
"INDEXED_ARRAY[1]": "one",
"INDEXED_ARRAY[2]": "two",
},
expectedArray: []string{},
},
{
template: "$ASTERIX_ARRAY[*]",
mapping: map[string]string{
"ASTERIX_ARRAY[*]": "asterix",
},
expectedArray: []string{},
},
}

for _, testcase := range testcases {
mapping := buildMapping(testcase.mapping)
result, err := Substitute(testcase.template, mapping)
assert.NilError(t, err)
assert.DeepEqual(t, testcase.expectedArray, result)
}
}

func TestInlinedArraysSubstitutions(t *testing.T) {
testcases := []struct {
rawValue string
expectedArray []string
}{
{
rawValue: "(zero one two)",
expectedArray: []string{"zero", "one", "two"},
},
{
rawValue: "()",
expectedArray: []string{},
},
{
rawValue: "( )",
expectedArray: []string{},
},
{
rawValue: "(\"zero\" \"one\" \"two\")",
expectedArray: []string{"zero", "one", "two"},
},
{
rawValue: "('zero' 'one' 'two')",
expectedArray: []string{"zero", "one", "two"},
},
{
rawValue: "(\"zero 0\" \"one 1\" \"two 2\")",
expectedArray: []string{"zero 0", "one 1", "two 2"},
},
{
rawValue: "(zero\\ 0 one\\ 1 two\\ 2)",
expectedArray: []string{"zero 0", "one 1", "two 2"},
},
{
rawValue: "(zero\\ 0 \"one 1\" two\\ 2)",
expectedArray: []string{"zero 0", "one 1", "two 2"},
},
{
rawValue: "( zero one two )",
expectedArray: []string{"zero", "one", "two"},
},
{
rawValue: "( '\"' )",
expectedArray: []string{"\""},
},
{
rawValue: "( \"'\" )",
expectedArray: []string{"'"},
},
{
rawValue: "( \\' )",
expectedArray: []string{"'"},
},
{
rawValue: "( \\\" )",
expectedArray: []string{"\""},
},
{
rawValue: "( \"\\\"\" )",
expectedArray: []string{"\""},
},
{
rawValue: "( '\\'' )",
expectedArray: []string{"'"},
},
{
rawValue: "(\\\\)",
expectedArray: []string{"\\"},
},
}

for _, testcase := range testcases {
mapping := buildMapping(map[string]string{"arr": testcase.rawValue})
result, err := Substitute("$arr[*]", mapping)
assert.NilError(t, err)
assert.DeepEqual(t, testcase.expectedArray, result)
}
}

func TestInlinedPriority(t *testing.T) {
testcases := []struct {
template string
mapping map[string]string
expectedArray []string
}{
{
template: "$MIXED_ARRAY[*]",
mapping: map[string]string{
"MIXED_ARRAY": "(zero one two)",
"MIXED_ARRAY[0]": "0",
"MIXED_ARRAY[1]": "1",
"MIXED_ARRAY[2]": "2",
},
expectedArray: []string{"zero", "one", "two"},
},
{
template: "$MIXED_ARRAY[*]",
mapping: map[string]string{
"MIXED_ARRAY": "(zero one two)",
"MIXED_ARRAY[3]": "3",
},
expectedArray: []string{"zero", "one", "two"},
},
}

for _, testcase := range testcases {
mapping := buildMapping(testcase.mapping)
result, err := Substitute(testcase.template, mapping)
assert.NilError(t, err)
assert.DeepEqual(t, testcase.expectedArray, result)
}
}

func TestBadInlinedDeclarations(t *testing.T) {
expectedErrMsg := func(value string, cause string) string {
return fmt.Sprintf(
"could not substitute array template \"$arr[*]\":\ninvalid array definition: \"%s\" - %s",
value,
cause,
)
}
testcases := []struct {
value string
cause string
}{
{value: "(", cause: "should be enclosed in parenthesis"},
{value: ")", cause: "should be enclosed in parenthesis"},
{value: "abc", cause: "should be enclosed in parenthesis"},
{value: "(zero", cause: "should be enclosed in parenthesis"},
{value: "zero)", cause: "should be enclosed in parenthesis"},
{value: "(zero one", cause: "should be enclosed in parenthesis"},
{value: "one zero)", cause: "should be enclosed in parenthesis"},
{value: "(zero one ", cause: "should be enclosed in parenthesis"},
{value: " one zero)", cause: "should be enclosed in parenthesis"},
{value: "(\")", cause: "quote not closed"},
{value: "(')", cause: "quote not closed"},
{value: "(zero one))", cause: "unescaped character (\")\")"},
{value: "((zero one)", cause: "unescaped character (\"(\")"},
{value: "(\"one)", cause: "quote not closed"},
{value: "(one\")", cause: "quote not closed"},
{value: "('one)", cause: "quote not closed"},
{value: "(one')", cause: "quote not closed"},
{value: "(\\)", cause: "nothing left to escape"},
}

for _, testcase := range testcases {
mapping := buildMapping(map[string]string{"arr": testcase.value})
result, err := Substitute("$arr[*]", mapping)
assert.DeepEqual(t, result, []string(nil))
assert.Error(t, err, expectedErrMsg(testcase.value, testcase.cause))
}
}

func TestBadTemplates(t *testing.T) {
testcases := []string{
"NO_DOLLAR_SIGN[*]",
"$NO_CLOSING_BRACKET[*",
"$NO_OPENING_BRACKET*]",
"$NO_OPENING_BRACE[*]}",
"${NO_CLOSING_BRACE[*]",
"${INDEX_OUTSIZE_BRACES}[*]",
}

for _, testcase := range testcases {
neverMapping := func(name string) (string, bool) { return "", false }
result, err := Substitute(testcase, neverMapping)
assert.DeepEqual(t, result, []string(nil))
assert.Error(t, err, fmt.Sprintf("not a valid array template: \"%s\"", testcase))
}
}
35 changes: 35 additions & 0 deletions arraytemplate/index_array_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2020 The Compose Specification Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package arraytemplate

import (
"fmt"
)

func getIndexed(arrayName string, mapping Mapping) []string {
arr := make([]string, 0, maxArraySize)
for i := 0; i < maxArraySize; i++ {
key := fmt.Sprintf("%s[%d]", arrayName, i)
value, ok := mapping(key)
if !ok {
break
}
arr = append(arr, value)
}

return arr
}
Loading