Skip to content

Commit

Permalink
fix: split documents
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Aug 23, 2024
1 parent c6862da commit 7b3d672
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 7 deletions.
18 changes: 11 additions & 7 deletions pkg/kube_resource/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,8 @@ func splitDocuments(s string) ([]string, error) {
return nil, fmt.Errorf("invalid document separator: %s", strings.TrimSpace(separator))
}
// Remove all whitespace
result := strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, s[prev:loc[0]])
if len(result) > 0 {
result := s[prev:loc[0]]
if len(result) > 0 && !isAllWhitespace(result) {
docs = append(docs, result)
}
prev = loc[1]
Expand All @@ -177,6 +172,15 @@ func splitDocuments(s string) ([]string, error) {
return docs, nil
}

func isAllWhitespace(str string) bool {
for _, r := range str {
if !unicode.IsSpace(r) {
return false
}
}
return true
}

// generate swagger model based on crd
func generate(crdYaml string) (*spec.Swagger, error) {
crdObj, _, err := scheme.Codecs.UniversalDeserializer().
Expand Down
112 changes: 112 additions & 0 deletions pkg/kube_resource/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,3 +631,115 @@ func TestSplitDocuments(t *testing.T) {
t.Errorf("splitDocuments failed. expected 2, got %d", len(files))
}
}

func TestSplitYamlStreamDocuments(t *testing.T) {
crds := `
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: foo.example.com
spec:
group: example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
field1:
type: string
scope: Namespaced
names:
plural: foo
singular: foo
kind: Foo
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: bar.example.com
spec:
group: example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
field2:
type: string
scope: Namespaced
names:
plural: bar
singular: bar
kind: Bar
`
files, _ := splitDocuments(crds)
if len(files) != 2 {
t.Errorf("splitDocuments failed. expected 2, got %d", len(files))
}
if files[0] != `
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: foo.example.com
spec:
group: example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
field1:
type: string
scope: Namespaced
names:
plural: foo
singular: foo
kind: Foo` {
panic(files[0])
}
if files[1] != `apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: bar.example.com
spec:
group: example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
field2:
type: string
scope: Namespaced
names:
plural: bar
singular: bar
kind: Bar
` {
panic(files[1])
}
}

0 comments on commit 7b3d672

Please sign in to comment.