Skip to content

Commit 073c322

Browse files
authored
refactor(model): remove unnecessary code for slice length check (#68)
Signed-off-by: lsytj0413 <[email protected]> Signed-off-by: lsytj0413 <[email protected]>
1 parent ddc15cc commit 073c322

File tree

3 files changed

+100
-8
lines changed

3 files changed

+100
-8
lines changed

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
4444
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4545
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
4646
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
47-
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
4847
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
4948
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d h1:3qF+Z8Hkrw9sOhrFHti9TlB1Hkac1x+DNRkv0XQiFjo=
5049
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=

model/auth.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ func init() {
3131
func AuthDefinitionsStructLevelValidation(structLevel validator.StructLevel) {
3232
authDefs := structLevel.Current().Interface().(AuthDefinitions)
3333
dict := map[string]bool{}
34-
if authDefs.Defs != nil && len(authDefs.Defs) > 1 {
35-
for _, a := range authDefs.Defs {
36-
if !dict[a.Name] {
37-
dict[a.Name] = true
38-
} else {
39-
structLevel.ReportError(reflect.ValueOf(a.Name), "Name", "name", "reqnameunique", "")
40-
}
34+
35+
for _, a := range authDefs.Defs {
36+
if !dict[a.Name] {
37+
dict[a.Name] = true
38+
} else {
39+
structLevel.ReportError(reflect.ValueOf(a.Name), "Name", "name", "reqnameunique", "")
4140
}
4241
}
4342
}

model/auth_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2021 The Serverless Workflow Specification Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package model
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
22+
val "github.com/serverlessworkflow/sdk-go/v2/validator"
23+
)
24+
25+
func TestAuthDefinitionsStructLevelValidation(t *testing.T) {
26+
type testCase struct {
27+
desp string
28+
authDefs AuthDefinitions
29+
err string
30+
}
31+
testCases := []testCase{
32+
{
33+
desp: "nil defs",
34+
authDefs: AuthDefinitions{
35+
Defs: nil,
36+
},
37+
err: ``,
38+
},
39+
{
40+
desp: "zero length defs",
41+
authDefs: AuthDefinitions{
42+
Defs: []Auth{},
43+
},
44+
err: ``,
45+
},
46+
{
47+
desp: "multi unique defs",
48+
authDefs: AuthDefinitions{
49+
Defs: []Auth{
50+
{
51+
Name: "1",
52+
},
53+
{
54+
Name: "2",
55+
},
56+
{
57+
Name: "3",
58+
},
59+
},
60+
},
61+
err: ``,
62+
},
63+
{
64+
desp: "multi non-unique defs",
65+
authDefs: AuthDefinitions{
66+
Defs: []Auth{
67+
{
68+
Name: "1",
69+
},
70+
{
71+
Name: "2",
72+
},
73+
{
74+
Name: "1",
75+
},
76+
},
77+
},
78+
err: `Key: 'AuthDefinitions.Name' Error:Field validation for 'Name' failed on the 'reqnameunique' tag`,
79+
},
80+
}
81+
for _, tc := range testCases {
82+
t.Run(tc.desp, func(t *testing.T) {
83+
err := val.GetValidator().Struct(tc.authDefs)
84+
85+
if tc.err != "" {
86+
assert.Error(t, err)
87+
assert.Regexp(t, tc.err, err)
88+
return
89+
}
90+
91+
assert.NoError(t, err)
92+
})
93+
}
94+
}

0 commit comments

Comments
 (0)