processMaps duplicates module configuration in the template for each value in a CSV. The external name for this is now "ForEach", but originally it was called "Map".
(originalContent []*yaml.Node, t *cft.Template, parentModule *Module)
| 132 | // each value in a CSV. The external name for this is now "ForEach", |
| 133 | // but originally it was called "Map". |
| 134 | func processMaps(originalContent []*yaml.Node, |
| 135 | t *cft.Template, parentModule *Module) ([]*yaml.Node, error) { |
| 136 | |
| 137 | content := make([]*yaml.Node, 0) |
| 138 | |
| 139 | // Process Maps, which duplicate the module for each element in a list |
| 140 | for i := 0; i < len(originalContent); i += 2 { |
| 141 | name := originalContent[i].Value |
| 142 | moduleConfig, err := t.ParseModuleConfig(name, originalContent[i+1]) |
| 143 | if err != nil { |
| 144 | return nil, err |
| 145 | } |
| 146 | |
| 147 | if moduleConfig.Map == nil { |
| 148 | content = append(content, originalContent[i]) |
| 149 | content = append(content, originalContent[i+1]) |
| 150 | continue |
| 151 | } |
| 152 | |
| 153 | keys, err := getMapKeys(moduleConfig, t, parentModule) |
| 154 | if err != nil { |
| 155 | return nil, err |
| 156 | } |
| 157 | |
| 158 | if len(keys) < 1 { |
| 159 | msg := "expected module Map to have items: %s" |
| 160 | mapErr := fmt.Errorf(msg, node.YamlStr(moduleConfig.Node)) |
| 161 | return nil, mapErr |
| 162 | } |
| 163 | |
| 164 | // Duplicate the config |
| 165 | for i, key := range keys { |
| 166 | mapName := fmt.Sprintf("%s%d", moduleConfig.Name, i) |
| 167 | |
| 168 | if moduleConfig.FnForEach != nil && |
| 169 | moduleConfig.FnForEach.OutputKeyHasIdentifier() { |
| 170 | // The OutputKey is something like A${Identifier}, |
| 171 | // which means we use the key instead of the |
| 172 | // array index to create the logical id |
| 173 | mapName = fmt.Sprintf("%s%s", moduleConfig.Name, key) |
| 174 | } |
| 175 | |
| 176 | copiedNode := node.Clone(moduleConfig.Node) |
| 177 | node.RemoveFromMap(copiedNode, ForEach) |
| 178 | copiedConfig, err := t.ParseModuleConfig(mapName, copiedNode) |
| 179 | if err != nil { |
| 180 | return nil, err |
| 181 | } |
| 182 | |
| 183 | // These values won't go into the YAML but we'll store them for |
| 184 | // later |
| 185 | |
| 186 | copiedConfig.OriginalName = moduleConfig.Name |
| 187 | copiedConfig.IsMapCopy = true |
| 188 | copiedConfig.MapIndex = i |
| 189 | copiedConfig.MapKey = key |
| 190 | |
| 191 | // Add a reference to the template so we can find it later for |
no test coverage detected