Skip to content

Commit

Permalink
Merge pull request #155 from michaelhtm/support/null
Browse files Browse the repository at this point in the history
Support null fields in ternary and field assignment
  • Loading branch information
a-hilaly authored Nov 21, 2024
2 parents dfa4d35 + aaeeeee commit 0583ea9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
2 changes: 2 additions & 0 deletions internal/cel/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func GoNativeType(v ref.Val) (interface{}, error) {
return v.ConvertToNative(reflect.TypeOf([]interface{}{}))
case types.MapType:
return v.ConvertToNative(reflect.TypeOf(map[string]interface{}{}))
case types.NullType:
return nil, nil
default:
// For types we can't convert, return as is with an error
return v.Value(), fmt.Errorf("unsupported type: %v", v.Type())
Expand Down
2 changes: 2 additions & 0 deletions internal/graph/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func parseResource(resource interface{}, schema *spec.Schema, path string) ([]va
return parseArray(field, schema, path, expectedType)
case string:
return parseString(field, schema, path, expectedType)
case nil:
return nil, nil
default:
return parseScalarTypes(field, schema, path, expectedType)
}
Expand Down
15 changes: 0 additions & 15 deletions internal/graph/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,6 @@ func TestTypeMismatches(t *testing.T) {
},
wantErr: true,
},
{
name: "Null value for non-nullable field",
resource: map[string]interface{}{
"nonNullableField": nil,
},
schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: map[string]spec.Schema{
"nonNullableField": {SchemaProps: spec.SchemaProps{Type: []string{"string"}}},
},
},
},
wantErr: true,
},
{
name: "Nil schema",
resource: map[string]interface{}{
Expand Down
34 changes: 33 additions & 1 deletion test/integration/suites/core/readiness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ var _ = Describe("Readiness", func() {
map[string]interface{}{
"name": "string",
"replicas": "integer",
"deployment": map[string]interface{}{
"includeAnnotations": "boolean | default=false",
"annotations": map[string]interface{}{
"app": "string | default=nginx",
},
},
"service": map[string]interface{}{
"includeAnnotations": "boolean | default=true",
"annotations": map[string]interface{}{
"app": "string | default=service",
},
},
},
nil,
),
Expand All @@ -69,6 +81,8 @@ var _ = Describe("Readiness", func() {
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "${schema.spec.name}",
"annotations": `${schema.spec.deployment.includeAnnotations == true
? schema.spec.deployment.annotations : null}`,
},
"spec": map[string]interface{}{
"replicas": "${schema.spec.replicas}",
Expand Down Expand Up @@ -105,6 +119,8 @@ var _ = Describe("Readiness", func() {
"kind": "Service",
"metadata": map[string]interface{}{
"name": "${deployment.metadata.name}",
"annotations": `${schema.spec.service.includeAnnotations == true
? schema.spec.service.annotations : null}`,
},
"spec": map[string]interface{}{
"selector": map[string]interface{}{
Expand Down Expand Up @@ -172,6 +188,16 @@ var _ = Describe("Readiness", func() {
"spec": map[string]interface{}{
"name": name,
"replicas": replicas,
"deployment": map[string]interface{}{
"includeAnnotations": false,
"annotations": map[string]interface{}{},
},
"service": map[string]interface{}{
"includeAnnotations": true,
"annotations": map[string]interface{}{
"app": "service",
},
},
},
},
}
Expand All @@ -198,6 +224,7 @@ var _ = Describe("Readiness", func() {
// Verify deployment specs
g.Expect(deployment.Spec.Template.Spec.Containers).To(HaveLen(1))
g.Expect(*deployment.Spec.Replicas).To(Equal(int32(replicas)))
g.Expect(deployment.Annotations).To(HaveLen(0))
}, 20*time.Second, time.Second).Should(Succeed())

// Verify Service is not created yet
Expand All @@ -223,13 +250,18 @@ var _ = Describe("Readiness", func() {
}
Expect(env.Client.Status().Update(ctx, deployment)).To(Succeed())

service := &corev1.Service{}
// Verify Service is created now
Eventually(func(g Gomega) {
err := env.Client.Get(ctx, types.NamespacedName{
Name: name,
Namespace: namespace,
}, &corev1.Service{})
}, service)
g.Expect(err).ToNot(HaveOccurred())

// validate service spec
Expect(service.Annotations).To(HaveLen(1))
Expect(service.Annotations["app"]).To(Equal("service"))
}, 20*time.Second, time.Second).Should(Succeed())

// Delete instance
Expand Down

0 comments on commit 0583ea9

Please sign in to comment.