Skip to content

Commit

Permalink
parser support for property access
Browse files Browse the repository at this point in the history
I couldn't figure out how to do this without basically rewriting it
  • Loading branch information
Craig Furman committed Jun 21, 2023
1 parent 5a6db3d commit a1d2c93
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 30 deletions.
21 changes: 20 additions & 1 deletion pkg/input/arm/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ type evaluationContext struct {
func newEvaluationContext() *evaluationContext {
return &evaluationContext{
funcs: map[string]armFnImpl{
"concat": concatImpl,
"concat": concatImpl,
"resourceGroup": resourceGroupImpl,
},
}
}
Expand Down Expand Up @@ -89,3 +90,21 @@ func concatImpl(args ...interface{}) (interface{}, error) {
}
return res, nil
}

// Return a stub
// https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-scope#resourcegroup
func resourceGroupImpl(args ...interface{}) (interface{}, error) {
if len(args) != 0 {
return nil, fmt.Errorf("expected zero args to resourceGroup(), got %d", len(args))
}

return map[string]interface{}{
"id": "stub-id",
"name": "stub-name",
"type": "stub-type",
"location": "stub-location",
"managedBy": "stub-managed-by",
"tags": map[string]interface{}{},
"properties": map[string]interface{}{},
}, nil
}
5 changes: 5 additions & 0 deletions pkg/input/arm/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func TestEvalARMTemplateStrings(t *testing.T) {
input: "[concat('foo', '-bar')]",
expected: "foo-bar",
},
{
name: "attribute access",
input: "[resourceGroup().location]",
expected: "stub-location",
},
} {
t.Run(tc.name, func(t *testing.T) {
val, err := EvaluateARMTemplateString(tc.input)
Expand Down
115 changes: 87 additions & 28 deletions pkg/input/arm/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,116 @@ import (
"fmt"
)

// TODO refactor to something that doesn't repeat that pop/peek a lot
func parse(tokens []token) (expression, error) {
return buildTree(&tokens)
p := &parser{remaining: tokens}
return p.parse()
}

func buildTree(remaining *[]token) (expression, error) {
if len(*remaining) == 0 {
type parser struct {
remaining []token
}

func (p *parser) parse() (expression, error) {
if p.done() {
return nil, errors.New("can't build expression from 0 tokens")
}

tkn := (*remaining)[0]
*remaining = (*remaining)[1:]
tkn := p.pop()

// TODO more scalar types
if strToken, ok := tkn.(stringLiteral); ok {
return stringLiteralExpr(strToken.content), nil
}

// If we reach here, we are building a function expression, because there are
// no "direct" identifier dereferences in ARM template expressions. The
// identifier is the function name.
idToken, ok := tkn.(identifier)
if !ok {
return nil, fmt.Errorf("expected token %#v to be an identifier", tkn)
}
tkn = (*remaining)[0]
*remaining = (*remaining)[1:]

// expect an open paren
if p.done() {
return nil, errors.New("expression cannot terminate with an identifier")
}
tkn = p.pop()
if _, ok := tkn.(openParen); !ok {
return nil, fmt.Errorf("expected token %#v to be a paren", tkn)
}

if _, ok := (*remaining)[0].(closeParen); ok {
*remaining = (*remaining)[1:]
return functionExpr{name: idToken.name}, nil
if p.done() {
return nil, errors.New("expression cannot terminate with an open paren")
}

// We are in a function
arg1, err := buildTree(remaining)
if err != nil {
return nil, err
}
args := []expression{arg1}
var args []expression
for {
tkn = (*remaining)[0]
*remaining = (*remaining)[1:]
tkn := p.peek()
if _, ok := tkn.(closeParen); ok {
return functionExpr{
name: idToken.name,
args: args,
}, nil
p.pop() // pop the close paren
expr := functionExpr{name: idToken.name, args: args}
if p.done() {
return expr, nil
}
if _, ok := p.peek().(dot); ok {
return p.buildPropertyAccessExpression(expr)
}
return expr, nil
}

if _, ok := tkn.(comma); !ok {
return nil, fmt.Errorf("expected token %#v to be a comma", tkn)
// There is a comma between args, so not before the first arg
if len(args) > 0 {
if _, ok := p.peek().(comma); !ok {
return nil, fmt.Errorf("expected token %#v to be a comma", tkn)
}
p.pop() // pop the comma
}

nextArg, err := buildTree(remaining)
nextArg, err := p.parse()
if err != nil {
return nil, err
}
args = append(args, nextArg)
}
}

func (p *parser) buildPropertyAccessExpression(expr expression) (expression, error) {
// we only enter this function from parse() if we peeked at a dot, so we know
// it gets past here at least once, and so always builds a real property
// access expression.
if p.done() {
return expr, nil
}
if _, ok := p.peek().(dot); !ok {
return expr, nil
}

p.pop() // pop the dot
if p.done() {
return nil, errors.New("expression cannot terminate with a dot")
}
tkn := p.pop()
nextPropChainElement, ok := tkn.(identifier)
if !ok {
return nil, fmt.Errorf("expected token %#v to be an identifier", tkn)
}
expr = propertyExpr{obj: expr, property: nextPropChainElement.name}
return p.buildPropertyAccessExpression(expr)
}

func (p *parser) peek() token {
return p.remaining[0]
}

func (p *parser) pop() token {
tkn := p.remaining[0]
p.remaining = p.remaining[1:]
return tkn
}

func (p *parser) done() bool {
return len(p.remaining) == 0
}

type expression interface {
eval(evalCtx evaluationContext) (interface{}, error)
}
Expand Down Expand Up @@ -117,5 +167,14 @@ type propertyExpr struct {
}

func (p propertyExpr) eval(evalCtx evaluationContext) (interface{}, error) {
return nil, nil
obj, err := p.obj.eval(evalCtx)
if err != nil {
return nil, err
}
objMap, ok := obj.(map[string]interface{})
if !ok {
return nil, errors.New("property access can only occur on objects")
}

return objMap[p.property], nil
}
24 changes: 23 additions & 1 deletion pkg/input/arm/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/stretchr/testify/require"
)

// TODO unbalanced parens / invalid expressions
// TODO unbalanced parens / invalid expressions error scenarios
func TestParse(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down Expand Up @@ -64,6 +64,28 @@ func TestParse(t *testing.T) {
},
},
},
{
name: "returns expression for property access",
input: "resourceGroup().location",
expected: propertyExpr{
obj: functionExpr{name: "resourceGroup"},
property: "location",
},
},
{
name: "returns expression for nested property access",
input: "resourceGroup().foo.bar.baz",
expected: propertyExpr{
obj: propertyExpr{
obj: propertyExpr{
obj: functionExpr{name: "resourceGroup"},
property: "foo",
},
property: "bar",
},
property: "baz",
},
},
} {
t.Run(tc.name, func(t *testing.T) {
tokens, err := tokenize(tc.input)
Expand Down

0 comments on commit a1d2c93

Please sign in to comment.