|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/terraform-providers/terraform-provider-datadog/generator/internal/model" |
| 9 | +) |
| 10 | + |
| 11 | +// OperationLocation identifies one operation participating in an artifact_name |
| 12 | +// collision. |
| 13 | +type OperationLocation struct { |
| 14 | + Path string |
| 15 | + Method string |
| 16 | + OperationId string |
| 17 | +} |
| 18 | + |
| 19 | +// ArtifactNameCollision records every operation that declared a given |
| 20 | +// (kind, artifact_name) pair, when more than one did. Uniqueness is scoped per |
| 21 | +// kind: Terraform resources and data sources occupy separate namespaces, so a |
| 22 | +// datadog_team resource and a datadog_team data source may coexist. |
| 23 | +type ArtifactNameCollision struct { |
| 24 | + Kind model.ArtifactKind |
| 25 | + Name string |
| 26 | + Sources []OperationLocation |
| 27 | +} |
| 28 | + |
| 29 | +// DuplicateArtifactNameError aggregates every (kind, artifact_name) collision |
| 30 | +// found across the spec into a single error, naming all source locations for |
| 31 | +// each. Collisions are sorted by (name, kind) and their sources by |
| 32 | +// (path, method), so the message is deterministic regardless of input order. |
| 33 | +type DuplicateArtifactNameError struct { |
| 34 | + Collisions []ArtifactNameCollision |
| 35 | +} |
| 36 | + |
| 37 | +func (e *DuplicateArtifactNameError) Error() string { |
| 38 | + var b strings.Builder |
| 39 | + b.WriteString("parser: duplicate artifact_name across operations:") |
| 40 | + for _, c := range e.Collisions { |
| 41 | + fmt.Fprintf(&b, "\n %s %q declared by:", c.Kind, c.Name) |
| 42 | + for _, s := range c.Sources { |
| 43 | + fmt.Fprintf(&b, "\n - %s %s (operationId %q)", s.Method, s.Path, s.OperationId) |
| 44 | + } |
| 45 | + } |
| 46 | + return b.String() |
| 47 | +} |
| 48 | + |
| 49 | +// CheckDuplicateArtifactNames reports every (kind, artifact_name) pair claimed |
| 50 | +// by more than one operation. Uniqueness is scoped per kind — Terraform |
| 51 | +// resources and data sources are separate namespaces, so the same name under |
| 52 | +// different kinds is allowed. Operations without tracking metadata are ignored |
| 53 | +// (they generate no artifact and so cannot collide). It returns a single |
| 54 | +// aggregated *DuplicateArtifactNameError naming every collision and all of its |
| 55 | +// sources, or nil when every name is unique within its kind. |
| 56 | +func CheckDuplicateArtifactNames(spec *model.Spec) error { |
| 57 | + // Key on (kind, name) rather than name alone. Keying on the kind value |
| 58 | + // itself — not an `== "resource"` special case — keeps this correct if new |
| 59 | + // artifact kinds are ever introduced. |
| 60 | + type artifactKey struct { |
| 61 | + Kind model.ArtifactKind |
| 62 | + Name string |
| 63 | + } |
| 64 | + byKey := make(map[artifactKey][]OperationLocation) |
| 65 | + for _, op := range spec.Operations { |
| 66 | + if op == nil || op.Tracking == nil { |
| 67 | + continue |
| 68 | + } |
| 69 | + key := artifactKey{Kind: op.Tracking.ArtifactKind, Name: op.Tracking.ArtifactName} |
| 70 | + byKey[key] = append(byKey[key], OperationLocation{ |
| 71 | + Path: op.Path, |
| 72 | + Method: op.Method, |
| 73 | + OperationId: op.OperationId, |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + var collisions []ArtifactNameCollision |
| 78 | + for key, locs := range byKey { |
| 79 | + if len(locs) < 2 { |
| 80 | + continue |
| 81 | + } |
| 82 | + sort.Slice(locs, func(i, j int) bool { |
| 83 | + if locs[i].Path != locs[j].Path { |
| 84 | + return locs[i].Path < locs[j].Path |
| 85 | + } |
| 86 | + return locs[i].Method < locs[j].Method |
| 87 | + }) |
| 88 | + collisions = append(collisions, ArtifactNameCollision{Kind: key.Kind, Name: key.Name, Sources: locs}) |
| 89 | + } |
| 90 | + if len(collisions) == 0 { |
| 91 | + return nil |
| 92 | + } |
| 93 | + sort.Slice(collisions, func(i, j int) bool { |
| 94 | + if collisions[i].Name != collisions[j].Name { |
| 95 | + return collisions[i].Name < collisions[j].Name |
| 96 | + } |
| 97 | + return collisions[i].Kind < collisions[j].Kind |
| 98 | + }) |
| 99 | + return &DuplicateArtifactNameError{Collisions: collisions} |
| 100 | +} |
0 commit comments