-
Notifications
You must be signed in to change notification settings - Fork 188
/
readable_conversion_endpoint_set.go
122 lines (103 loc) · 4.36 KB
/
readable_conversion_endpoint_set.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package conversions
import (
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
)
// ReadableConversionEndpointSet is a set of uniquely named readable conversion endpoints
type ReadableConversionEndpointSet map[string]*ReadableConversionEndpoint
// NewReadableConversionEndpointSet returns a new set of readable conversion endpoints
func NewReadableConversionEndpointSet() ReadableConversionEndpointSet {
return make(ReadableConversionEndpointSet)
}
// CreatePropertyEndpoints will create readable conversion endpoints for any properties found on the passed instance
// type. Existing endpoints won't be overwritten. Returns the count of new endpoints created
func (set ReadableConversionEndpointSet) CreatePropertyEndpoints(sourceType astmodel.Type) int {
// Add an endpoint for each property we can read
return set.addForEachProperty(sourceType, func(prop *astmodel.PropertyDefinition) *ReadableConversionEndpoint {
return NewReadableConversionEndpointReadingProperty(prop.PropertyName(), prop.PropertyType())
})
}
// CreateValueFunctionEndpoints will create additional readable conversion endpoints for any compatible functions found
// on the passed instance type that don't collide with the names of existing endpoints. Returns the count of new
// endpoints created
func (set ReadableConversionEndpointSet) CreateValueFunctionEndpoints(sourceType astmodel.Type) int {
// Add more endpoints for any value functions we can read
return set.addForEachValueFunction(sourceType, func(fn astmodel.ValueFunction) *ReadableConversionEndpoint {
return NewReadableConversionEndpointReadingValueFunction(fn.Name(), fn.ReturnType())
})
}
// addForEachProperty iterates over the properties defined by the instance and uses the supplied factory func to
// create an endpoint for each one. Existing endpoints will NOT be overwritten. If a property containing a PropertyBag
// is found, it will be skipped as property bags are special-cased elsewhere.
// Returns the count of new endpoints created.
func (set ReadableConversionEndpointSet) addForEachProperty(
instance astmodel.Type,
factory func(definition *astmodel.PropertyDefinition) *ReadableConversionEndpoint,
) int {
count := 0
if container, ok := astmodel.AsPropertyContainer(instance); ok {
// Construct a set containing the properties we can assign
// This is made up of all regular properties, plus specific kinds of embedded properties
properties := container.Properties().Copy()
typesToCopy := astmodel.NewTypeNameSet(astmodel.ObjectMetaType)
for _, prop := range container.EmbeddedProperties() {
name, ok := astmodel.AsTypeName(prop.PropertyType())
if !ok {
// We only expect to get embedded type names, but skip any others just in case
continue
}
if !typesToCopy.Contains(name) {
// Not a type we need to copy
continue
}
properties.Add(prop.WithName(astmodel.PropertyName(name.Name())))
}
for _, prop := range properties {
name := string(prop.PropertyName())
if _, defined := set[name]; defined {
// Don't overwrite any existing endpoints
continue
}
if prop.PropertyType().Equals(astmodel.PropertyBagType, astmodel.EqualityOverrides{}) {
// We don't create endpoints for property bag properties, they're special cased elsewhere
continue
}
endpoint := factory(prop)
set[name] = endpoint
count++
}
}
return count
}
// addForEachValueFunction iterates over the functions defined by the instance and uses the supplied factory func to
// create an endpoint for each one. Existing endpoints will NOT be overwritten.
// Returns the count of new endpoints created.
func (set ReadableConversionEndpointSet) addForEachValueFunction(
instance astmodel.Type,
factory func(definition astmodel.ValueFunction) *ReadableConversionEndpoint,
) int {
count := 0
if container, ok := astmodel.AsFunctionContainer(instance); ok {
for _, fn := range container.Functions() {
name := fn.Name()
if _, defined := set[name]; defined {
// Don't overwrite any existing endpoints
continue
}
valueFn, ok := fn.(astmodel.ValueFunction)
if ok {
endpoint := factory(valueFn)
set[name] = endpoint
count++
}
}
}
return count
}
// Delete removes a specific endpoint from the set
func (set ReadableConversionEndpointSet) Delete(name string) {
delete(set, name)
}