-
Notifications
You must be signed in to change notification settings - Fork 24
/
expr_one_of_ref_origins.go
59 lines (49 loc) · 1.61 KB
/
expr_one_of_ref_origins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package decoder
import (
"context"
"github.com/hashicorp/hcl-lang/reference"
)
func (oo OneOf) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
origins := make(reference.Origins, 0)
for _, con := range oo.cons {
expr := newExpression(oo.pathCtx, oo.expr, con)
e, ok := expr.(ReferenceOriginsExpression)
if !ok {
continue
}
origins = appendOrigins(origins, e.ReferenceOrigins(ctx, allowSelfRefs))
}
return origins
}
func appendOrigins(origins, newOrigins reference.Origins) reference.Origins {
// Deduplicating origins like this is probably not ideal
// from performance perspective (N^2) but improving it would
// require redesign of the schema.Reference constraint,
// such that it doesn't necessitate the need of OneOf for multiple ScopeIds
// and maintains all possible ScopeIds & Types as a *single* slice.
for _, newOrigin := range newOrigins {
newMatchableOrigin, ok := newOrigin.(reference.MatchableOrigin)
if !ok {
origins = append(origins, newOrigin)
continue
}
foundMatch := false
for i, origin := range origins {
existingOrigin, ok := origin.(reference.MatchableOrigin)
if ok &&
existingOrigin.Address().Equals(newMatchableOrigin.Address()) &&
rangesEqual(existingOrigin.OriginRange(), newMatchableOrigin.OriginRange()) {
// TODO? deduplicate constraints
origins[i] = existingOrigin.AppendConstraints(newMatchableOrigin.OriginConstraints())
foundMatch = true
break
}
}
if !foundMatch {
origins = append(origins, newOrigin)
}
}
return origins
}