/
property_access.go
104 lines (87 loc) · 2.38 KB
/
property_access.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package config
import "github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
// propertyAccess provides access to a specific property's configuration.
type propertyAccess[T any] struct {
model *ObjectModelConfiguration
accessor func(*PropertyConfiguration) *configurable[T]
fallback *typeAccess[T]
}
// makePropertyAccess creates a new propertyAccess[T] for the given model and accessor function
func makePropertyAccess[T any](
model *ObjectModelConfiguration,
accessor func(*PropertyConfiguration,
) *configurable[T],
) propertyAccess[T] {
return propertyAccess[T]{
model: model,
accessor: accessor,
}
}
// Lookup returns the configured value for the given type name and property name
func (a *propertyAccess[T]) Lookup(
name astmodel.InternalTypeName,
property astmodel.PropertyName,
) (T, bool) {
result, ok := a.lookupCore(name, property)
if ok {
return result, true
}
if a.fallback != nil {
return a.fallback.Lookup(name)
}
return result, false
}
// lookupCore is the core implementation of lookup
func (a *propertyAccess[T]) lookupCore(
name astmodel.InternalTypeName,
property astmodel.PropertyName,
) (T, bool) {
var c *configurable[T]
visitor := newSinglePropertyConfigurationVisitor(
name,
property,
func(configuration *PropertyConfiguration) error {
c = a.accessor(configuration)
return nil
})
err := visitor.visit(a.model)
if err != nil {
// Something went wrong; this is unexpected and shouldn't happen
panic(err)
}
if c == nil {
var zero T
return zero, false
}
return c.Lookup()
}
// VerifyConsumed ensures that all configured values have been consumed
func (a *propertyAccess[T]) VerifyConsumed() error {
visitor := newEveryPropertyConfigurationVisitor(
func(configuration *PropertyConfiguration) error {
c := a.accessor(configuration)
return c.VerifyConsumed()
})
err := visitor.visit(a.model)
if err != nil {
return err
}
if a.fallback != nil {
return a.fallback.VerifyConsumed()
}
return nil
}
// MarkUnconsumed marks all configured values as unconsumed
func (a *propertyAccess[T]) MarkUnconsumed() error {
visitor := newEveryPropertyConfigurationVisitor(
func(configuration *PropertyConfiguration) error {
c := a.accessor(configuration)
c.MarkUnconsumed()
return nil
})
return visitor.visit(a.model)
}