...
1
16
17 package explain
18
19 import (
20 "fmt"
21
22 "k8s.io/kube-openapi/pkg/util/proto"
23 )
24
25
26
27 type fieldLookup struct {
28
29 Path []string
30
31
32 Schema proto.Schema
33 Error error
34 }
35
36
37
38 func (f *fieldLookup) SaveLeafSchema(schema proto.Schema) bool {
39 if len(f.Path) != 0 {
40 return false
41 }
42
43 f.Schema = schema
44
45 return true
46 }
47
48
49 func (f *fieldLookup) VisitArray(a *proto.Array) {
50 if f.SaveLeafSchema(a) {
51 return
52 }
53
54
55 a.SubType.Accept(f)
56 }
57
58
59 func (f *fieldLookup) VisitMap(m *proto.Map) {
60 if f.SaveLeafSchema(m) {
61 return
62 }
63
64
65 m.SubType.Accept(f)
66 }
67
68
69
70 func (f *fieldLookup) VisitPrimitive(p *proto.Primitive) {
71
72
73 f.Schema = p
74 }
75
76
77 func (f *fieldLookup) VisitKind(k *proto.Kind) {
78 if f.SaveLeafSchema(k) {
79 return
80 }
81
82 subSchema, ok := k.Fields[f.Path[0]]
83 if !ok {
84 f.Error = fmt.Errorf("field %q does not exist", f.Path[0])
85 return
86 }
87
88 f.Path = f.Path[1:]
89 subSchema.Accept(f)
90 }
91
92 func (f *fieldLookup) VisitArbitrary(a *proto.Arbitrary) {
93 f.Schema = a
94 }
95
96
97 func (f *fieldLookup) VisitReference(r proto.Reference) {
98 if f.SaveLeafSchema(r) {
99 return
100 }
101
102
103 r.SubSchema().Accept(f)
104 }
105
106
107 func LookupSchemaForField(schema proto.Schema, path []string) (proto.Schema, error) {
108 f := &fieldLookup{Path: path}
109 schema.Accept(f)
110 return f.Schema, f.Error
111 }
112
View as plain text