Skip to content

Commit a7c3ccd

Browse files
committed
Add ParsableType.FromStructured
1 parent ced6cfa commit a7c3ccd

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

typed/parser.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,28 @@ func (p ParseableType) FromYAML(object YAMLObject) (*TypedValue, error) {
102102
return AsTyped(value.NewValueInterface(v), p.Schema, p.TypeRef)
103103
}
104104

105-
// FromUnstructured converts a go interface to a TypedValue. It will return an
105+
// FromUnstructured converts a go "interface{}" type, typically an
106+
// unstructured object in Kubernetes world. to a TypedValue. It returns an
106107
// error if the resulting object fails schema validation.
108+
// The provided interface{} must be one of: map[string]interface{},
109+
// map[interface{}]interface{}, []interface{}, int types, float types,
110+
// string or boolean. Nested interface{} must also be one of these types.
107111
func (p ParseableType) FromUnstructured(in interface{}) (*TypedValue, error) {
108112
return AsTyped(value.NewValueInterface(in), p.Schema, p.TypeRef)
109113
}
110114

115+
// FromStructured converts a go "interface{}" type, typically an structured object in
116+
// Kubernetes, to a TypedValue. It will return an
117+
// error if the resulting object fails schema validation.
118+
// The provided "interface{}" may contain structs and types that are converted to Values
119+
// // by the jsonMarshaler interface.
120+
func (p ParseableType) FromStructured(in interface{}) (*TypedValue, error) {
121+
v, err := value.NewValueReflect(in)
122+
if err != nil {
123+
return nil, fmt.Errorf("error creating struct value reflector: %v", err)
124+
}
125+
return AsTyped(v, p.Schema, p.TypeRef)
126+
}
111127
// DeducedParseableType is a ParseableType that deduces the type from
112128
// the content of the object.
113129
var DeducedParseableType ParseableType = createOrDie(YAMLObject(`types:

value/mapreflect.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (r mapReflect) Delete(key string) {
5454
val.SetMapIndex(r.toMapKey(key), reflect.Value{})
5555
}
5656

57-
// TODO: Do we need to support types that implement json.Marshaler and are used as string keys
57+
// TODO: Do we need to support types that implement json.Marshaler and are used as string keys?
5858
func (r mapReflect) toMapKey(key string) reflect.Value {
5959
val := r.Value
6060
return reflect.ValueOf(key).Convert(val.Type().Key())

value/valuereflect.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var reflectPool = sync.Pool{
3232

3333
// NewValueReflect creates a Value backed by an "interface{}" type,
3434
// typically an structured object in Kubernetes world that is uses reflection to expose.
35-
// The provide "interface{}" may contain structs and types that are converted to Values
35+
// The provided "interface{}" may contain structs and types that are converted to Values
3636
// by the jsonMarshaler interface.
3737
func NewValueReflect(value interface{}) (Value, error) {
3838
if value == nil {

0 commit comments

Comments
 (0)