Skip to content

Commit ff58d58

Browse files
author
Antoine Pelisse
committed
Create DeducedType for schemafree types
Some types don't have a schema (CRDs), and so we're creating the schema on the fly for these values. We also build the infrastructure to create these types easily and similarly to how we handled existing types.
1 parent 6daeeda commit ff58d58

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

typed/parser.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,34 @@ func (p *parseableType) FromUnstructured(in interface{}) (TypedValue, error) {
114114
}
115115
return AsTyped(v, &p.parser.Schema, p.typename)
116116
}
117+
118+
// DeducedParseableType is a ParseableType that deduces the type from
119+
// the content of the object.
120+
type DeducedParseableType struct{}
121+
122+
var _ ParseableType = DeducedParseableType{}
123+
124+
// IsValid always returns true for a DeducedParseableType.
125+
func (p DeducedParseableType) IsValid() bool {
126+
return true
127+
}
128+
129+
// FromYAML parses a yaml string into an object and deduces the type for
130+
// that object.
131+
func (p DeducedParseableType) FromYAML(object YAMLObject) (TypedValue, error) {
132+
v, err := value.FromYAML([]byte(object))
133+
if err != nil {
134+
return TypedValue{}, err
135+
}
136+
return AsTypedDeduced(v), nil
137+
}
138+
139+
// FromUnstructured converts a go interface to a TypedValue. It will return an
140+
// error if the input object uses un-handled types.
141+
func (p DeducedParseableType) FromUnstructured(in interface{}) (TypedValue, error) {
142+
v, err := value.FromUnstructured(in)
143+
if err != nil {
144+
return TypedValue{}, err
145+
}
146+
return AsTypedDeduced(v), nil
147+
}

typed/typed.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ func AsTyped(v value.Value, s *schema.Schema, typeName string) (TypedValue, erro
4747
return tv, nil
4848
}
4949

50+
// AsTypedDeduced is going to generate it's own type definition based on
51+
// the content of the object. This is useful for CRDs that don't have a
52+
// validation field.
53+
func AsTypedDeduced(v value.Value) TypedValue {
54+
return TypedValue{
55+
value: v,
56+
typeRef: schema.TypeRefFromValue(v),
57+
schema: nil,
58+
}
59+
}
60+
5061
// AsValue removes the type from the TypedValue and only keeps the value.
5162
func (tv TypedValue) AsValue() *value.Value {
5263
return &tv.value

0 commit comments

Comments
 (0)