|
9 | 9 | "os" |
10 | 10 | "path/filepath" |
11 | 11 | "sort" |
| 12 | + "strings" |
12 | 13 |
|
13 | 14 | hcl "github.com/hashicorp/hcl/v2" |
14 | 15 | "github.com/hashicorp/hcl/v2/gohcl" |
@@ -80,7 +81,13 @@ func main() { |
80 | 81 | if shapeName == "any" { |
81 | 82 | continue |
82 | 83 | } |
83 | | - model := shapes[shapeName].(map[string]interface{}) |
| 84 | + |
| 85 | + model := findShape(shapes, shapeName) |
| 86 | + if model == nil { |
| 87 | + fmt.Printf("Shape `%s` not found, skipping\n", shapeName) |
| 88 | + continue |
| 89 | + } |
| 90 | + |
84 | 91 | schema, err := fetchSchema(mapping.Resource, attribute, model, awsProvider) |
85 | 92 | if err != nil { |
86 | 93 | fmt.Fprintf(os.Stderr, "Error processing `%s.%s`: %v\n", mapping.Resource, attribute, err) |
@@ -153,3 +160,119 @@ func validMapping(model map[string]interface{}) bool { |
153 | 160 | return false |
154 | 161 | } |
155 | 162 | } |
| 163 | + |
| 164 | +// findShape locates a shape in Smithy format with namespace-qualified lookup |
| 165 | +func findShape(shapes map[string]interface{}, shapeName string) map[string]interface{} { |
| 166 | + // Try with service namespace qualification (Smithy format) |
| 167 | + serviceNamespace := extractServiceNamespace(shapes) |
| 168 | + if serviceNamespace != "" { |
| 169 | + qualifiedName := fmt.Sprintf("%s#%s", serviceNamespace, shapeName) |
| 170 | + if shape, ok := shapes[qualifiedName]; ok { |
| 171 | + return convertSmithyShape(shape.(map[string]interface{})) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Fallback to direct lookup (legacy format or unqualified shapes) |
| 176 | + if shape, ok := shapes[shapeName]; ok { |
| 177 | + if shapeMap, ok := shape.(map[string]interface{}); ok { |
| 178 | + return shapeMap |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return nil |
| 183 | +} |
| 184 | + |
| 185 | +// extractServiceNamespace extracts the namespace from the Smithy service definition |
| 186 | +func extractServiceNamespace(shapes map[string]interface{}) string { |
| 187 | + for shapeName, shape := range shapes { |
| 188 | + if shapeMap, ok := shape.(map[string]interface{}); ok { |
| 189 | + if shapeType, ok := shapeMap["type"].(string); ok && shapeType == "service" { |
| 190 | + // Extract namespace from shape name (e.g., "com.amazonaws.acmpca#ACMPrivateCA") |
| 191 | + if parts := strings.Split(shapeName, "#"); len(parts) == 2 { |
| 192 | + return parts[0] |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + return "" |
| 198 | +} |
| 199 | + |
| 200 | +// convertSmithyShape converts Smithy model format to internal format |
| 201 | +// Smithy uses traits for metadata while our internal format uses direct fields |
| 202 | +func convertSmithyShape(smithyShape map[string]interface{}) map[string]interface{} { |
| 203 | + result := make(map[string]interface{}) |
| 204 | + |
| 205 | + // Copy type |
| 206 | + if shapeType, ok := smithyShape["type"]; ok { |
| 207 | + result["type"] = shapeType |
| 208 | + } |
| 209 | + |
| 210 | + // Extract constraints and patterns from Smithy traits |
| 211 | + if traits, ok := smithyShape["traits"].(map[string]interface{}); ok { |
| 212 | + // Length constraints |
| 213 | + if lengthTrait, ok := traits["smithy.api#length"].(map[string]interface{}); ok { |
| 214 | + if min, ok := lengthTrait["min"]; ok { |
| 215 | + result["min"] = min |
| 216 | + } |
| 217 | + if max, ok := lengthTrait["max"]; ok { |
| 218 | + result["max"] = max |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + // Pattern constraint |
| 223 | + if pattern, ok := traits["smithy.api#pattern"].(string); ok { |
| 224 | + result["pattern"] = pattern |
| 225 | + } |
| 226 | + |
| 227 | + // Enum as trait (older Smithy style) |
| 228 | + if enumTrait, ok := traits["smithy.api#enum"]; ok { |
| 229 | + if enumList, ok := enumTrait.([]interface{}); ok { |
| 230 | + enumValues := make([]string, 0, len(enumList)) |
| 231 | + for _, enumItem := range enumList { |
| 232 | + if enumMap, ok := enumItem.(map[string]interface{}); ok { |
| 233 | + if value, ok := enumMap["value"].(string); ok { |
| 234 | + enumValues = append(enumValues, value) |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + sort.Strings(enumValues) |
| 239 | + result["enum"] = enumValues |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + // Enum as type (newer Smithy style: type="enum" with members) |
| 245 | + if shapeType, ok := smithyShape["type"].(string); ok && shapeType == "enum" { |
| 246 | + if members, ok := smithyShape["members"].(map[string]interface{}); ok { |
| 247 | + enumValues := make([]string, 0, len(members)) |
| 248 | + |
| 249 | + // Sort member names for deterministic ordering |
| 250 | + memberNames := make([]string, 0, len(members)) |
| 251 | + for memberName := range members { |
| 252 | + memberNames = append(memberNames, memberName) |
| 253 | + } |
| 254 | + sort.Strings(memberNames) |
| 255 | + |
| 256 | + // Extract enum values |
| 257 | + for _, memberName := range memberNames { |
| 258 | + memberData := members[memberName] |
| 259 | + enumValue := memberName |
| 260 | + |
| 261 | + // Check for explicit enumValue in traits |
| 262 | + if memberMap, ok := memberData.(map[string]interface{}); ok { |
| 263 | + if traits, ok := memberMap["traits"].(map[string]interface{}); ok { |
| 264 | + if enumValueTrait, ok := traits["smithy.api#enumValue"].(string); ok { |
| 265 | + enumValue = enumValueTrait |
| 266 | + } |
| 267 | + } |
| 268 | + } |
| 269 | + enumValues = append(enumValues, enumValue) |
| 270 | + } |
| 271 | + |
| 272 | + result["enum"] = enumValues |
| 273 | + result["type"] = "string" // Normalize enum type to string |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + return result |
| 278 | +} |
0 commit comments