Skip to content

Commit 8f9bdaf

Browse files
author
jennybuckley
committed
schema from value function
1 parent 203764c commit 8f9bdaf

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

schema/fromvalue.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package schema
18+
19+
import (
20+
"sigs.k8s.io/structured-merge-diff/value"
21+
)
22+
23+
// FromValue creates a schema from a value v
24+
func FromValue(name string, v value.Value) *Schema {
25+
atom := atomFor(v)
26+
typeDef := TypeDef{
27+
Name: name,
28+
Atom: atom,
29+
}
30+
return &Schema{
31+
Types: []TypeDef{typeDef},
32+
}
33+
}
34+
35+
func atomFor(v value.Value) Atom {
36+
switch {
37+
// Untyped cases (handled at the bottom of this function)
38+
case v.Null:
39+
case v.ListValue != nil:
40+
case v.FloatValue != nil:
41+
case v.IntValue != nil:
42+
case v.StringValue != nil:
43+
case v.BooleanValue != nil:
44+
// Recursive case
45+
case v.MapValue != nil:
46+
s := Struct{}
47+
for i := range v.MapValue.Items {
48+
child := v.MapValue.Items[i]
49+
field := StructField{
50+
Name: child.Name,
51+
Type: TypeRef{
52+
Inlined: atomFor(child.Value),
53+
},
54+
}
55+
s.Fields = append(s.Fields, field)
56+
}
57+
return Atom{Struct: &s}
58+
}
59+
60+
return Atom{Untyped: &Untyped{}}
61+
}

schema/fromvalue_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package schema_test
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
"gopkg.in/yaml.v2"
24+
"sigs.k8s.io/structured-merge-diff/schema"
25+
"sigs.k8s.io/structured-merge-diff/value"
26+
)
27+
28+
const (
29+
typeName = "test"
30+
)
31+
32+
func TestFromValue(t *testing.T) {
33+
34+
table := []struct {
35+
objYAML string
36+
schema string
37+
}{
38+
{`a: a`, `
39+
types:
40+
- name: ` + typeName + `
41+
struct:
42+
fields:
43+
- name: a
44+
type:
45+
untyped: {}`},
46+
{`{"a": [{"a": null}]}`, `
47+
types:
48+
- name: ` + typeName + `
49+
struct:
50+
fields:
51+
- name: a
52+
type:
53+
untyped: {}`},
54+
{`{"a": null}`, `
55+
types:
56+
- name: ` + typeName + `
57+
struct:
58+
fields:
59+
- name: a
60+
type:
61+
untyped: {}`},
62+
{`{"q": {"y": 6, "b": [7, 8, 9]}}`, `
63+
types:
64+
- name: ` + typeName + `
65+
struct:
66+
fields:
67+
- name: q
68+
type:
69+
struct:
70+
fields:
71+
- name: y
72+
type:
73+
untyped: {}
74+
- name: b
75+
type:
76+
untyped: {}`},
77+
}
78+
79+
for _, tt := range table {
80+
tt := tt
81+
t.Run(tt.objYAML, func(t *testing.T) {
82+
t.Parallel()
83+
v, err := value.FromYAML([]byte(tt.objYAML))
84+
if err != nil {
85+
t.Fatalf("couldn't parse: %v", err)
86+
}
87+
got := schema.FromValue(typeName, v)
88+
89+
expected := &schema.Schema{}
90+
err = yaml.Unmarshal([]byte(tt.schema), expected)
91+
if err != nil {
92+
t.Fatalf("couldn't parse: %v", err)
93+
}
94+
95+
if !reflect.DeepEqual(got, expected) {
96+
t.Errorf("wanted\n%+v\nbut got\n%+v\n", expected, got)
97+
}
98+
})
99+
}
100+
}

0 commit comments

Comments
 (0)