Skip to content

Commit a0a5303

Browse files
authored
Merge pull request #56 from jennybuckley/value-to-schema
generate schema from value function (for crds)
2 parents 4a66966 + 1fce0a0 commit a0a5303

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

schema/fromvalue.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
// TypeRefFromValue creates an inlined type from a value v
24+
func TypeRefFromValue(v value.Value) TypeRef {
25+
atom := atomFor(v)
26+
return TypeRef{
27+
Inlined: atom,
28+
}
29+
}
30+
31+
func atomFor(v value.Value) Atom {
32+
switch {
33+
// Untyped cases (handled at the bottom of this function)
34+
case v.Null:
35+
case v.ListValue != nil:
36+
case v.FloatValue != nil:
37+
case v.IntValue != nil:
38+
case v.StringValue != nil:
39+
case v.BooleanValue != nil:
40+
// Recursive case
41+
case v.MapValue != nil:
42+
s := Struct{}
43+
for i := range v.MapValue.Items {
44+
child := v.MapValue.Items[i]
45+
field := StructField{
46+
Name: child.Name,
47+
Type: TypeRef{
48+
Inlined: atomFor(child.Value),
49+
},
50+
}
51+
s.Fields = append(s.Fields, field)
52+
}
53+
return Atom{Struct: &s}
54+
}
55+
56+
return Atom{Untyped: &Untyped{}}
57+
}

schema/fromvalue_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
func TestTypeRefFromValue(t *testing.T) {
29+
30+
table := []struct {
31+
objYAML string
32+
typeRef string
33+
}{
34+
{`a: a`, `
35+
struct:
36+
fields:
37+
- name: a
38+
type:
39+
untyped: {}`},
40+
{`{"a": [{"a": null}]}`, `
41+
struct:
42+
fields:
43+
- name: a
44+
type:
45+
untyped: {}`},
46+
{`{"a": null}`, `
47+
struct:
48+
fields:
49+
- name: a
50+
type:
51+
untyped: {}`},
52+
{`{"q": {"y": 6, "b": [7, 8, 9]}}`, `
53+
struct:
54+
fields:
55+
- name: q
56+
type:
57+
struct:
58+
fields:
59+
- name: y
60+
type:
61+
untyped: {}
62+
- name: b
63+
type:
64+
untyped: {}`},
65+
}
66+
67+
for _, tt := range table {
68+
tt := tt
69+
t.Run(tt.objYAML, func(t *testing.T) {
70+
t.Parallel()
71+
v, err := value.FromYAML([]byte(tt.objYAML))
72+
if err != nil {
73+
t.Fatalf("couldn't parse: %v", err)
74+
}
75+
got := schema.TypeRefFromValue(v)
76+
77+
expected := schema.TypeRef{}
78+
err = yaml.Unmarshal([]byte(tt.typeRef), &expected)
79+
if err != nil {
80+
t.Fatalf("couldn't parse: %v", err)
81+
}
82+
83+
if !reflect.DeepEqual(got, expected) {
84+
t.Errorf("wanted\n%+v\nbut got\n%+v\n", expected, got)
85+
}
86+
})
87+
}
88+
}

0 commit comments

Comments
 (0)