This is an automated email from the ASF dual-hosted git repository. kezhenxu94 pushed a commit to branch func/notEmpty/nil in repository https://gitbox.apache.org/repos/asf/skywalking-infra-e2e.git
commit f75836a52f1f7055f28881cb928971e5c3a048aa Author: kezhenxu94 <[email protected]> AuthorDate: Wed Sep 14 22:03:00 2022 +0800 verifier: notEmpty should be able to handle nil --- internal/components/verifier/funcs.go | 13 ++++++++++--- internal/components/verifier/verifier_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/internal/components/verifier/funcs.go b/internal/components/verifier/funcs.go index 785a7b5..e4925cb 100644 --- a/internal/components/verifier/funcs.go +++ b/internal/components/verifier/funcs.go @@ -69,9 +69,16 @@ 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) + } + switch s := s.(type) { + case string: + if len(strings.TrimSpace(s)) > 0 { + return s + } + return fmt.Sprintf("<%q is empty, wanted is not empty>", s) } return fmt.Sprintf("<%q is empty, wanted is not empty>", s) } 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 {
