Skip to content

Commit 2ee5933

Browse files
André R. de Mirandaspolti
andauthored
Refactor in favor to use primitiveOrStruct. (#159)
* change unmarshalString to primitiveOrStruct Signed-off-by: André R. de Miranda <andre@galgo.tech> * Add unit test for DefaultCondition UnmarshalJSON Signed-off-by: André R. de Miranda <andre@galgo.tech> * Improve switch case Signed-off-by: André R. de Miranda <andre@galgo.tech> * Change unmarshall array/object/file and change error messages Signed-off-by: André R. de Miranda <andre@galgo.tech> * Check response.Write error Signed-off-by: André R. de Miranda <andre@galgo.tech> * Move default values to the function ApplyDefault() Signed-off-by: André R. de Miranda <andre@galgo.tech> * Change the go version to 1.20 on workflows Signed-off-by: André R. de Miranda <andre@galgo.tech> * uri scheme empty the default is file:// Signed-off-by: André R. de Miranda <andre@galgo.tech> * Change the golang version to 1.20.2. Fix lint Signed-off-by: André R. de Miranda <andre@galgo.tech> * Rebase from main branch Signed-off-by: André R. de Miranda <andre@galgo.tech> * Revision suggestions. Add a new interface for the types used in const, that aux the validation and error handling. Signed-off-by: André R. de Miranda <andre@galgo.tech> * Refactor unmarshalObjectOrFile and unmarshalArrayOrFile Signed-off-by: André R. de Miranda <andre@galgo.tech> * Revision suggestions. Rename interface Kinds to Kind Signed-off-by: André R. de Miranda <andre@galgo.tech> * Accept external resource as object or array Signed-off-by: André R. de Miranda <andre@galgo.tech> * Revision suggestions. Support golang 1.19 and external resource Signed-off-by: André R. de Miranda <andre@galgo.tech> * Revision suggestions Signed-off-by: André R. de Miranda <andre@galgo.tech> * Revision suggestions Signed-off-by: André R. de Miranda <andre@galgo.tech> * Revision suggestions. Add more tests Signed-off-by: André R. de Miranda <andre@galgo.tech> * Remove tag #nosec in model/util.go Co-authored-by: Filippe Spolti <filippespolti@gmail.com> Signed-off-by: André R. de Miranda <andre@galgo.tech> * Rename AuthArray to Auths, and ErrorArray to Errors. Improve loadExternalResource. Clean state json. Add state type validation oneofkind Signed-off-by: André R. de Miranda <andre@galgo.tech> * Return json tags in state Signed-off-by: André R. de Miranda <andre@galgo.tech> --------- Signed-off-by: André R. de Miranda <andre@galgo.tech> Co-authored-by: Filippe Spolti <filippespolti@gmail.com>
1 parent b74604f commit 2ee5933

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1519
-967
lines changed

.github/workflows/Go-SDK-Check-k8s-integration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
- name: Checkout Code
3333
uses: actions/checkout@v3
3434
- name: Setup Go ${{ env.GO_VERSION }}
35-
uses: actions/setup-go@v3
35+
uses: actions/setup-go@v4
3636
with:
3737
go-version: ${{ env.GO_VERSION }}
3838
id: go

.github/workflows/Go-SDK-PR-Check.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
- name: Checkout Code
3333
uses: actions/checkout@v3
3434
- name: Setup Go ${{ env.GO_VERSION }}
35-
uses: actions/setup-go@v3
35+
uses: actions/setup-go@v4
3636
with:
3737
go-version: ${{ env.GO_VERSION }}
3838
id: go

model/action.go

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414

1515
package model
1616

17-
import (
18-
"bytes"
19-
"encoding/json"
20-
"fmt"
21-
)
22-
2317
// Action specify invocations of services or other workflows during workflow execution.
2418
type Action struct {
2519
// Defines Unique action identifier.
@@ -62,21 +56,17 @@ type Action struct {
6256
Condition string `json:"condition,omitempty"`
6357
}
6458

65-
type actionForUnmarshal Action
59+
type actionUnmarshal Action
6660

6761
// UnmarshalJSON implements json.Unmarshaler
6862
func (a *Action) UnmarshalJSON(data []byte) error {
63+
a.ApplyDefault()
64+
return unmarshalObject("action", data, (*actionUnmarshal)(a))
65+
}
6966

70-
v := actionForUnmarshal{
71-
ActionDataFilter: ActionDataFilter{UseResults: true},
72-
}
73-
err := json.Unmarshal(data, &v)
74-
if err != nil {
75-
return fmt.Errorf("action value '%s' is not supported, it must be an object or string", string(data))
76-
}
77-
*a = Action(v)
78-
79-
return nil
67+
// ApplyDefault set the default values for Action
68+
func (a *Action) ApplyDefault() {
69+
a.ActionDataFilter.ApplyDefault()
8070
}
8171

8272
// FunctionRef defines the reference to a reusable function definition
@@ -95,40 +85,20 @@ type FunctionRef struct {
9585
// Specifies if the function should be invoked sync or async. Default is sync.
9686
// +kubebuilder:validation:Enum=async;sync
9787
// +kubebuilder:default=sync
98-
Invoke InvokeKind `json:"invoke,omitempty" validate:"required,oneof=async sync"`
88+
Invoke InvokeKind `json:"invoke,omitempty" validate:"required,oneofkind"`
9989
}
10090

101-
type functionRefForUnmarshal FunctionRef
91+
type functionRefUnmarshal FunctionRef
10292

10393
// UnmarshalJSON implements json.Unmarshaler
10494
func (f *FunctionRef) UnmarshalJSON(data []byte) error {
105-
data = bytes.TrimSpace(data)
106-
if len(data) == 0 {
107-
return fmt.Errorf("no bytes to unmarshal")
108-
}
109-
110-
var err error
111-
switch data[0] {
112-
case '"':
113-
f.RefName, err = unmarshalString(data)
114-
if err != nil {
115-
return err
116-
}
117-
f.Invoke = InvokeKindSync
118-
return nil
119-
case '{':
120-
v := functionRefForUnmarshal{
121-
Invoke: InvokeKindSync,
122-
}
123-
err = json.Unmarshal(data, &v)
124-
if err != nil {
125-
return fmt.Errorf("functionRef value '%s' is not supported, it must be an object or string", string(data))
126-
}
127-
*f = FunctionRef(v)
128-
return nil
129-
}
95+
f.ApplyDefault()
96+
return unmarshalPrimitiveOrObject("functionRef", data, &f.RefName, (*functionRefUnmarshal)(f))
97+
}
13098

131-
return fmt.Errorf("functionRef value '%s' is not supported, it must be an object or string", string(data))
99+
// ApplyDefault set the default values for Function Ref
100+
func (f *FunctionRef) ApplyDefault() {
101+
f.Invoke = InvokeKindSync
132102
}
133103

134104
// Sleep defines time periods workflow execution should sleep before & after function execution
@@ -142,3 +112,10 @@ type Sleep struct {
142112
// +optional
143113
After string `json:"after,omitempty" validate:"omitempty,iso8601duration"`
144114
}
115+
116+
type sleepUnmarshal Sleep
117+
118+
// UnmarshalJSON implements json.Unmarshaler
119+
func (s *Sleep) UnmarshalJSON(data []byte) error {
120+
return unmarshalObject("sleep", data, (*sleepUnmarshal)(s))
121+
}

model/action_data_filter.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414

1515
package model
1616

17-
import (
18-
"bytes"
19-
"encoding/json"
20-
"fmt"
21-
)
22-
2317
// ActionDataFilter used to filter action data results.
2418
// +optional
2519
// +optional
@@ -41,24 +35,15 @@ type ActionDataFilter struct {
4135
ToStateData string `json:"toStateData,omitempty"`
4236
}
4337

44-
type actionDataFilterForUnmarshal ActionDataFilter
38+
type actionDataFilterUnmarshal ActionDataFilter
4539

4640
// UnmarshalJSON implements json.Unmarshaler
47-
func (f *ActionDataFilter) UnmarshalJSON(data []byte) error {
48-
data = bytes.TrimSpace(data)
49-
if len(data) == 0 {
50-
return fmt.Errorf("no bytes to unmarshal")
51-
}
52-
53-
v := actionDataFilterForUnmarshal{
54-
UseResults: true,
55-
}
56-
err := json.Unmarshal(data, &v)
57-
if err != nil {
58-
// TODO: replace the error message with correct type's name
59-
return err
60-
}
41+
func (a *ActionDataFilter) UnmarshalJSON(data []byte) error {
42+
a.ApplyDefault()
43+
return unmarshalObject("actionDataFilter", data, (*actionDataFilterUnmarshal)(a))
44+
}
6145

62-
*f = ActionDataFilter(v)
63-
return nil
46+
// ApplyDefault set the default values for Action Data Filter
47+
func (a *ActionDataFilter) ApplyDefault() {
48+
a.UseResults = true
6449
}

model/action_data_filter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestActionDataFilterUnmarshalJSON(t *testing.T) {
6161
desp: "invalid json format",
6262
data: `{"fromStateData": 1, "results": "2", "toStateData": "3"}`,
6363
expect: ActionDataFilter{},
64-
err: `json: cannot unmarshal number into Go struct field actionDataFilterForUnmarshal.fromStateData of type string`,
64+
err: `actionDataFilter.fromStateData must be string`,
6565
},
6666
}
6767

model/action_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package model
1616

1717
import (
18+
"encoding/json"
1819
"testing"
1920

2021
"github.com/stretchr/testify/assert"
@@ -92,3 +93,64 @@ func TestSleepValidate(t *testing.T) {
9293
})
9394
}
9495
}
96+
97+
func TestFunctionRefUnmarshalJSON(t *testing.T) {
98+
type testCase struct {
99+
desp string
100+
data string
101+
expect FunctionRef
102+
err string
103+
}
104+
105+
testCases := []testCase{
106+
{
107+
desp: "invalid object refName",
108+
data: `{"refName": 1}`,
109+
expect: FunctionRef{},
110+
err: "functionRef.refName must be string",
111+
},
112+
{
113+
desp: "object with refName",
114+
data: `{"refName": "function name"}`,
115+
expect: FunctionRef{
116+
RefName: "function name",
117+
Invoke: InvokeKindSync,
118+
},
119+
err: ``,
120+
},
121+
{
122+
desp: "object with refName and Invoke",
123+
data: `{"refName": "function name", "invoke": "async"}`,
124+
expect: FunctionRef{
125+
RefName: "function name",
126+
Invoke: InvokeKindAsync,
127+
},
128+
err: ``,
129+
},
130+
{
131+
desp: "refName string",
132+
data: `"function name"`,
133+
expect: FunctionRef{
134+
RefName: "function name",
135+
Invoke: InvokeKindSync,
136+
},
137+
err: ``,
138+
},
139+
}
140+
141+
for _, tc := range testCases[:1] {
142+
t.Run(tc.desp, func(t *testing.T) {
143+
var v FunctionRef
144+
err := json.Unmarshal([]byte(tc.data), &v)
145+
146+
if tc.err != "" {
147+
assert.Error(t, err)
148+
assert.Equal(t, tc.err, err.Error())
149+
return
150+
}
151+
152+
assert.NoError(t, err)
153+
assert.Equal(t, tc.expect, v)
154+
})
155+
}
156+
}

0 commit comments

Comments
 (0)