Skip to content

Commit

Permalink
Add a bit more resiliency for unmarshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
mweagle committed Mar 10, 2018
1 parent d5ef69e commit e9aa8d8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
24 changes: 22 additions & 2 deletions func_select.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cloudformation

import "encoding/json"
import (
"encoding/json"
"fmt"
)

type selectArg interface{}

Expand Down Expand Up @@ -47,9 +50,26 @@ func (f *SelectFunc) UnmarshalJSON(data []byte) error {
if len(v.FnSelect) != 2 {
return &json.UnsupportedValueError{Str: string(data)}
}
if err := json.Unmarshal(v.FnSelect[0], &f.Selector); err != nil {
// Possible that the second arg is another template, in which case
// we need to check for that...Example:
/*
"Fn::Select": [
0,
{
"Fn::GetAtt": [
"ApplicationLoadBalancer",
"SecurityGroups"
]
}
]
*/
var positionSelector interface{}
if err := json.Unmarshal(v.FnSelect[0], &positionSelector); err != nil {
return err
}
f.Selector = fmt.Sprintf("%v", positionSelector)

// What about the second one?
if err := json.Unmarshal(v.FnSelect[1], &f.Items); err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions string_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func (x *StringListExpr) UnmarshalJSON(data []byte) error {
x.Func = stringFunc
return nil
}

// Possible that it's a Fn::GetAtt instance?
getAttrFunc, ok := funcCall.(GetAttFunc)
if ok {
x.Func = StringList(getAttrFunc)
return nil
}
return fmt.Errorf("%#v is not a StringListFunc", funcCall)
}

Expand Down

0 comments on commit e9aa8d8

Please sign in to comment.