This is an automated email from the ASF dual-hosted git repository.
kezhenxu94 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-infra-e2e.git
The following commit(s) were added to refs/heads/main by this push:
new 81fab7d verifier: `notEmpty` should be able to handle nil (#86)
81fab7d is described below
commit 81fab7dbf5bfb201166d7d8e0089f99d2dc761f3
Author: kezhenxu94 <[email protected]>
AuthorDate: Thu Sep 15 10:07:46 2022 +0800
verifier: `notEmpty` should be able to handle nil (#86)
---
internal/components/verifier/funcs.go | 14 ++++++++++----
internal/components/verifier/verifier_test.go | 25 +++++++++++++++++++++++++
2 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/internal/components/verifier/funcs.go
b/internal/components/verifier/funcs.go
index 785a7b5..59155e8 100644
--- a/internal/components/verifier/funcs.go
+++ b/internal/components/verifier/funcs.go
@@ -69,11 +69,17 @@ func sha512encode(s string) string {
return hex.EncodeToString(hash.Sum(nil))
}
-func notEmpty(s string) string {
- if len(strings.TrimSpace(s)) > 0 {
- return s
+func notEmpty(s interface{}) string {
+ if s == nil {
+ return fmt.Sprintf("<%q is empty, wanted is not empty>", s)
}
- return fmt.Sprintf("<%q is empty, wanted is not empty>", s)
+ if s, ok := s.(string); ok {
+ if len(strings.TrimSpace(s)) > 0 {
+ return s
+ }
+ return fmt.Sprintf("<%q is empty, wanted is not empty>", s)
+ }
+ return fmt.Sprintf("notEmpty only supports nil or string type, but was
%T", s)
}
func regexpMatch(s, pattern string) string {
diff --git a/internal/components/verifier/verifier_test.go
b/internal/components/verifier/verifier_test.go
index 09da74c..167b5d1 100644
--- a/internal/components/verifier/verifier_test.go
+++ b/internal/components/verifier/verifier_test.go
@@ -271,6 +271,31 @@ metrics:
`,
},
wantErr: false,
+ }, {
+ name: "notEmpty with nil",
+ args: args{
+ actualData: `
+- key: 0
+ value:
+ - key: name
+ value: SET TIMESTAMP
+ - key: id
+ value: "123"
+ - key: refid
+ value: null
+ `,
+ expectedTemplate: `
+{{- contains . }}
+- key: 0
+ value:
+ {{- contains .value }}
+ - key: {{ notEmpty .key }}
+ value: {{ notEmpty .value }}
+ {{- end }}
+{{- end }}
+ `,
+ },
+ wantErr: false,
},
}
for _, tt := range tests {