Skip to content

Commit 5ce8e2b

Browse files
authored
fix: split documents (#120)
Signed-off-by: peefy <[email protected]>
1 parent c6862da commit 5ce8e2b

File tree

2 files changed

+123
-7
lines changed

2 files changed

+123
-7
lines changed

pkg/kube_resource/generator/generator.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,8 @@ func splitDocuments(s string) ([]string, error) {
161161
return nil, fmt.Errorf("invalid document separator: %s", strings.TrimSpace(separator))
162162
}
163163
// Remove all whitespace
164-
result := strings.Map(func(r rune) rune {
165-
if unicode.IsSpace(r) {
166-
return -1
167-
}
168-
return r
169-
}, s[prev:loc[0]])
170-
if len(result) > 0 {
164+
result := s[prev:loc[0]]
165+
if len(result) > 0 && !isAllWhitespace(result) {
171166
docs = append(docs, result)
172167
}
173168
prev = loc[1]
@@ -177,6 +172,15 @@ func splitDocuments(s string) ([]string, error) {
177172
return docs, nil
178173
}
179174

175+
func isAllWhitespace(str string) bool {
176+
for _, r := range str {
177+
if !unicode.IsSpace(r) {
178+
return false
179+
}
180+
}
181+
return true
182+
}
183+
180184
// generate swagger model based on crd
181185
func generate(crdYaml string) (*spec.Swagger, error) {
182186
crdObj, _, err := scheme.Codecs.UniversalDeserializer().

pkg/kube_resource/generator/generator_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,115 @@ func TestSplitDocuments(t *testing.T) {
631631
t.Errorf("splitDocuments failed. expected 2, got %d", len(files))
632632
}
633633
}
634+
635+
func TestSplitYamlStreamDocuments(t *testing.T) {
636+
crds := `
637+
apiVersion: apiextensions.k8s.io/v1
638+
kind: CustomResourceDefinition
639+
metadata:
640+
name: foo.example.com
641+
spec:
642+
group: example.com
643+
versions:
644+
- name: v1
645+
served: true
646+
storage: true
647+
schema:
648+
openAPIV3Schema:
649+
type: object
650+
properties:
651+
spec:
652+
type: object
653+
properties:
654+
field1:
655+
type: string
656+
scope: Namespaced
657+
names:
658+
plural: foo
659+
singular: foo
660+
kind: Foo
661+
---
662+
apiVersion: apiextensions.k8s.io/v1
663+
kind: CustomResourceDefinition
664+
metadata:
665+
name: bar.example.com
666+
spec:
667+
group: example.com
668+
versions:
669+
- name: v1
670+
served: true
671+
storage: true
672+
schema:
673+
openAPIV3Schema:
674+
type: object
675+
properties:
676+
spec:
677+
type: object
678+
properties:
679+
field2:
680+
type: string
681+
scope: Namespaced
682+
names:
683+
plural: bar
684+
singular: bar
685+
kind: Bar
686+
`
687+
files, _ := splitDocuments(crds)
688+
if len(files) != 2 {
689+
t.Errorf("splitDocuments failed. expected 2, got %d", len(files))
690+
}
691+
if files[0] != `
692+
apiVersion: apiextensions.k8s.io/v1
693+
kind: CustomResourceDefinition
694+
metadata:
695+
name: foo.example.com
696+
spec:
697+
group: example.com
698+
versions:
699+
- name: v1
700+
served: true
701+
storage: true
702+
schema:
703+
openAPIV3Schema:
704+
type: object
705+
properties:
706+
spec:
707+
type: object
708+
properties:
709+
field1:
710+
type: string
711+
scope: Namespaced
712+
names:
713+
plural: foo
714+
singular: foo
715+
kind: Foo` {
716+
panic(files[0])
717+
}
718+
if files[1] != `apiVersion: apiextensions.k8s.io/v1
719+
kind: CustomResourceDefinition
720+
metadata:
721+
name: bar.example.com
722+
spec:
723+
group: example.com
724+
versions:
725+
- name: v1
726+
served: true
727+
storage: true
728+
schema:
729+
openAPIV3Schema:
730+
type: object
731+
properties:
732+
spec:
733+
type: object
734+
properties:
735+
field2:
736+
type: string
737+
scope: Namespaced
738+
names:
739+
plural: bar
740+
singular: bar
741+
kind: Bar
742+
` {
743+
panic(files[1])
744+
}
745+
}

0 commit comments

Comments
 (0)