Add missing VisitArbitrary methods in kubectl explain

pull/564/head
Maciej Szulik 2019-02-13 20:16:26 +01:00
parent 01e7b3040a
commit 22a3f6de5e
No known key found for this signature in database
GPG Key ID: F15E55D276FA84C4
5 changed files with 109 additions and 0 deletions

View File

@ -89,6 +89,10 @@ func (f *fieldLookup) VisitKind(k *proto.Kind) {
subSchema.Accept(f) subSchema.Accept(f)
} }
func (f *fieldLookup) VisitArbitrary(a *proto.Arbitrary) {
f.Schema = a
}
// VisitReference is mostly a passthrough. // VisitReference is mostly a passthrough.
func (f *fieldLookup) VisitReference(r proto.Reference) { func (f *fieldLookup) VisitReference(r proto.Reference) {
if f.SaveLeafSchema(r) { if f.SaveLeafSchema(r) {

View File

@ -87,3 +87,48 @@ func TestFindField(t *testing.T) {
}) })
} }
} }
func TestCrdFindField(t *testing.T) {
schema := resources.LookupResource(schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "CrdKind",
})
if schema == nil {
t.Fatal("Counldn't find schema v1.CrdKind")
}
tests := []struct {
name string
path []string
err string
expectedPath string
}{
{
name: "test1",
path: []string{},
expectedPath: "CrdKind",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path, err := LookupSchemaForField(schema, tt.path)
gotErr := ""
if err != nil {
gotErr = err.Error()
}
gotPath := ""
if path != nil {
gotPath = path.GetPath().String()
}
if gotErr != tt.err || gotPath != tt.expectedPath {
t.Errorf("LookupSchemaForField(schema, %v) = (path: %q, err: %q), expected (path: %q, err: %q)",
tt.path, gotPath, gotErr, tt.expectedPath, tt.err)
}
})
}
}

View File

@ -56,10 +56,12 @@ func (m *modelPrinter) PrintDescription(schema proto.Schema) error {
if err := m.Writer.Write("DESCRIPTION:"); err != nil { if err := m.Writer.Write("DESCRIPTION:"); err != nil {
return err return err
} }
empty := true
for i, desc := range append(m.Descriptions, schema.GetDescription()) { for i, desc := range append(m.Descriptions, schema.GetDescription()) {
if desc == "" { if desc == "" {
continue continue
} }
empty = false
if i != 0 { if i != 0 {
if err := m.Writer.Write(""); err != nil { if err := m.Writer.Write(""); err != nil {
return err return err
@ -69,6 +71,9 @@ func (m *modelPrinter) PrintDescription(schema proto.Schema) error {
return err return err
} }
} }
if empty {
return m.Writer.Indent(descriptionIndentLevel).WriteWrapped("<empty>")
}
return nil return nil
} }
@ -134,6 +139,15 @@ func (m *modelPrinter) VisitPrimitive(p *proto.Primitive) {
m.Error = m.PrintDescription(p) m.Error = m.PrintDescription(p)
} }
func (m *modelPrinter) VisitArbitrary(a *proto.Arbitrary) {
if err := m.PrintKindAndVersion(); err != nil {
m.Error = err
return
}
m.Error = m.PrintDescription(a)
}
// VisitReference recurses inside the subtype, while collecting the description. // VisitReference recurses inside the subtype, while collecting the description.
func (m *modelPrinter) VisitReference(r proto.Reference) { func (m *modelPrinter) VisitReference(r proto.Reference) {
m.Descriptions = append(m.Descriptions, r.GetDescription()) m.Descriptions = append(m.Descriptions, r.GetDescription())

View File

@ -133,3 +133,40 @@ DESCRIPTION:
} }
} }
} }
func TestCRDModel(t *testing.T) {
gvk := schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "CrdKind",
}
schema := resources.LookupResource(gvk)
if schema == nil {
t.Fatal("Couldn't find schema v1.CrdKind")
}
tests := []struct {
path []string
want string
}{
{
path: []string{},
want: `KIND: CrdKind
VERSION: v1
DESCRIPTION:
<empty>
`,
},
}
for _, test := range tests {
buf := bytes.Buffer{}
if err := PrintModelDescription(test.path, &buf, schema, gvk, false); err != nil {
t.Fatalf("Failed to PrintModelDescription for path %v: %v", test.path, err)
}
got := buf.String()
if got != test.want {
t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), test.want)
}
}
}

View File

@ -73,6 +73,15 @@
"$ref": "#/definitions/PrimitiveDef" "$ref": "#/definitions/PrimitiveDef"
} }
} }
},
"CrdKind": {
"x-kubernetes-group-version-kind": [
{
"group": "",
"kind": "CrdKind",
"version": "v1"
}
]
} }
} }
} }