Skip to content

Commit 0583ea9

Browse files
authored
Merge pull request #155 from michaelhtm/support/null
Support null fields in ternary and field assignment
2 parents dfa4d35 + aaeeeee commit 0583ea9

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

internal/cel/conversions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func GoNativeType(v ref.Val) (interface{}, error) {
4444
return v.ConvertToNative(reflect.TypeOf([]interface{}{}))
4545
case types.MapType:
4646
return v.ConvertToNative(reflect.TypeOf(map[string]interface{}{}))
47+
case types.NullType:
48+
return nil, nil
4749
default:
4850
// For types we can't convert, return as is with an error
4951
return v.Value(), fmt.Errorf("unsupported type: %v", v.Type())

internal/graph/parser/parser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func parseResource(resource interface{}, schema *spec.Schema, path string) ([]va
5454
return parseArray(field, schema, path, expectedType)
5555
case string:
5656
return parseString(field, schema, path, expectedType)
57+
case nil:
58+
return nil, nil
5759
default:
5860
return parseScalarTypes(field, schema, path, expectedType)
5961
}

internal/graph/parser/parser_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,21 +245,6 @@ func TestTypeMismatches(t *testing.T) {
245245
},
246246
wantErr: true,
247247
},
248-
{
249-
name: "Null value for non-nullable field",
250-
resource: map[string]interface{}{
251-
"nonNullableField": nil,
252-
},
253-
schema: &spec.Schema{
254-
SchemaProps: spec.SchemaProps{
255-
Type: []string{"object"},
256-
Properties: map[string]spec.Schema{
257-
"nonNullableField": {SchemaProps: spec.SchemaProps{Type: []string{"string"}}},
258-
},
259-
},
260-
},
261-
wantErr: true,
262-
},
263248
{
264249
name: "Nil schema",
265250
resource: map[string]interface{}{

test/integration/suites/core/readiness_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ var _ = Describe("Readiness", func() {
6060
map[string]interface{}{
6161
"name": "string",
6262
"replicas": "integer",
63+
"deployment": map[string]interface{}{
64+
"includeAnnotations": "boolean | default=false",
65+
"annotations": map[string]interface{}{
66+
"app": "string | default=nginx",
67+
},
68+
},
69+
"service": map[string]interface{}{
70+
"includeAnnotations": "boolean | default=true",
71+
"annotations": map[string]interface{}{
72+
"app": "string | default=service",
73+
},
74+
},
6375
},
6476
nil,
6577
),
@@ -69,6 +81,8 @@ var _ = Describe("Readiness", func() {
6981
"kind": "Deployment",
7082
"metadata": map[string]interface{}{
7183
"name": "${schema.spec.name}",
84+
"annotations": `${schema.spec.deployment.includeAnnotations == true
85+
? schema.spec.deployment.annotations : null}`,
7286
},
7387
"spec": map[string]interface{}{
7488
"replicas": "${schema.spec.replicas}",
@@ -105,6 +119,8 @@ var _ = Describe("Readiness", func() {
105119
"kind": "Service",
106120
"metadata": map[string]interface{}{
107121
"name": "${deployment.metadata.name}",
122+
"annotations": `${schema.spec.service.includeAnnotations == true
123+
? schema.spec.service.annotations : null}`,
108124
},
109125
"spec": map[string]interface{}{
110126
"selector": map[string]interface{}{
@@ -172,6 +188,16 @@ var _ = Describe("Readiness", func() {
172188
"spec": map[string]interface{}{
173189
"name": name,
174190
"replicas": replicas,
191+
"deployment": map[string]interface{}{
192+
"includeAnnotations": false,
193+
"annotations": map[string]interface{}{},
194+
},
195+
"service": map[string]interface{}{
196+
"includeAnnotations": true,
197+
"annotations": map[string]interface{}{
198+
"app": "service",
199+
},
200+
},
175201
},
176202
},
177203
}
@@ -198,6 +224,7 @@ var _ = Describe("Readiness", func() {
198224
// Verify deployment specs
199225
g.Expect(deployment.Spec.Template.Spec.Containers).To(HaveLen(1))
200226
g.Expect(*deployment.Spec.Replicas).To(Equal(int32(replicas)))
227+
g.Expect(deployment.Annotations).To(HaveLen(0))
201228
}, 20*time.Second, time.Second).Should(Succeed())
202229

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

253+
service := &corev1.Service{}
226254
// Verify Service is created now
227255
Eventually(func(g Gomega) {
228256
err := env.Client.Get(ctx, types.NamespacedName{
229257
Name: name,
230258
Namespace: namespace,
231-
}, &corev1.Service{})
259+
}, service)
232260
g.Expect(err).ToNot(HaveOccurred())
261+
262+
// validate service spec
263+
Expect(service.Annotations).To(HaveLen(1))
264+
Expect(service.Annotations["app"]).To(Equal("service"))
233265
}, 20*time.Second, time.Second).Should(Succeed())
234266

235267
// Delete instance

0 commit comments

Comments
 (0)