MCPcopy Create free account
hub / github.com/aws-cloudformation/rain / getMapKeys

Function getMapKeys

cft/pkg/maps.go:71–129  ·  view source on GitHub ↗

getMapKeys gets the CSV key values from either a hard-coded string or from a Ref.

(moduleConfig *cft.ModuleConfig, t *cft.Template,
	parentModule *Module)

Source from the content-addressed store, hash-verified

69// getMapKeys gets the CSV key values from either a hard-coded
70// string or from a Ref.
71func getMapKeys(moduleConfig *cft.ModuleConfig, t *cft.Template,
72 parentModule *Module) ([]string, error) {
73
74 // The map is either a CSV or a Ref to a CSV that we can fully
75 // resolve
76 mapJson := node.ToSJson(moduleConfig.Map)
77 var keys []string
78 if moduleConfig.Map.Kind == yaml.ScalarNode {
79 keys = node.StringsFromNode(moduleConfig.Map)
80 } else if moduleConfig.Map.Kind == yaml.SequenceNode {
81 keys = node.StringsFromNode(moduleConfig.Map)
82 } else if moduleConfig.Map.Kind == yaml.MappingNode {
83 if cft.IsRef(moduleConfig.Map) {
84 r := moduleConfig.Map.Content[1].Value
85 // Look in the parent templates Parameters for the Ref
86 if !t.HasSection(cft.Parameters) {
87 msg := "module Map Ref no Parameters: %s"
88 return nil, fmt.Errorf(msg, mapJson)
89 }
90 params, _ := t.GetSection(cft.Parameters)
91 _, keysNode, _ := s11n.GetMapValue(params, r)
92 if keysNode == nil {
93 msg := "expected module Map Ref to a Parameter: %s"
94 return nil, fmt.Errorf(msg, mapJson)
95 }
96
97 // Look at the parent module Properties
98 // TODO: Will this work in nested modules?
99 // Have those props been resolved?
100 if parentModule != nil {
101 if parentVal, ok := parentModule.Config.Properties()[r]; ok {
102 csv, ok := parentVal.(string)
103 if ok {
104 keys = strings.Split(csv, ",")
105 } else {
106 msg := "expected Map keys to be a CSV: %v"
107 return nil, fmt.Errorf(msg, parentVal)
108 }
109 }
110 }
111
112 if len(keys) == 0 {
113 _, d, _ := s11n.GetMapValue(keysNode, "Default")
114 if d == nil {
115 msg := "expected module Map Ref to a Default: %s"
116 return nil, fmt.Errorf(msg, mapJson)
117 }
118 keys = node.StringsFromNode(d)
119 }
120 } else {
121 msg := "expected module Map to be a Ref: %s"
122 return nil, fmt.Errorf(msg, mapJson)
123 }
124 } else {
125 return nil, fmt.Errorf("unexpected module Map Kind: %s", mapJson)
126 }
127
128 return keys, nil

Callers 1

processMapsFunction · 0.85

Calls 7

ToSJsonFunction · 0.92
StringsFromNodeFunction · 0.92
IsRefFunction · 0.92
GetMapValueFunction · 0.92
HasSectionMethod · 0.80
GetSectionMethod · 0.80
PropertiesMethod · 0.80

Tested by

no test coverage detected