Skip to content

Refactor in favor to use primitiveOrStruct. #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/Go-SDK-Check-k8s-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Checkout Code
uses: actions/checkout@v3
- name: Setup Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
id: go
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/Go-SDK-PR-Check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Checkout Code
uses: actions/checkout@v3
- name: Setup Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
id: go
Expand Down
67 changes: 22 additions & 45 deletions model/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

package model

import (
"bytes"
"encoding/json"
"fmt"
)

// Action specify invocations of services or other workflows during workflow execution.
type Action struct {
// Defines Unique action identifier.
Expand Down Expand Up @@ -62,21 +56,17 @@ type Action struct {
Condition string `json:"condition,omitempty"`
}

type actionForUnmarshal Action
type actionUnmarshal Action

// UnmarshalJSON implements json.Unmarshaler
func (a *Action) UnmarshalJSON(data []byte) error {
a.ApplyDefault()
return unmarshalObject("action", data, (*actionUnmarshal)(a))
}

v := actionForUnmarshal{
ActionDataFilter: ActionDataFilter{UseResults: true},
}
err := json.Unmarshal(data, &v)
if err != nil {
return fmt.Errorf("action value '%s' is not supported, it must be an object or string", string(data))
}
*a = Action(v)

return nil
// ApplyDefault set the default values for Action
func (a *Action) ApplyDefault() {
a.ActionDataFilter.ApplyDefault()
}

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

type functionRefForUnmarshal FunctionRef
type functionRefUnmarshal FunctionRef

// UnmarshalJSON implements json.Unmarshaler
func (f *FunctionRef) UnmarshalJSON(data []byte) error {
data = bytes.TrimSpace(data)
if len(data) == 0 {
return fmt.Errorf("no bytes to unmarshal")
}

var err error
switch data[0] {
case '"':
f.RefName, err = unmarshalString(data)
if err != nil {
return err
}
f.Invoke = InvokeKindSync
return nil
case '{':
v := functionRefForUnmarshal{
Invoke: InvokeKindSync,
}
err = json.Unmarshal(data, &v)
if err != nil {
return fmt.Errorf("functionRef value '%s' is not supported, it must be an object or string", string(data))
}
*f = FunctionRef(v)
return nil
}
f.ApplyDefault()
return unmarshalPrimitiveOrObject("functionRef", data, &f.RefName, (*functionRefUnmarshal)(f))
}

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

// Sleep defines time periods workflow execution should sleep before & after function execution
Expand All @@ -142,3 +112,10 @@ type Sleep struct {
// +optional
After string `json:"after,omitempty" validate:"omitempty,iso8601duration"`
}

type sleepUnmarshal Sleep

// UnmarshalJSON implements json.Unmarshaler
func (s *Sleep) UnmarshalJSON(data []byte) error {
return unmarshalObject("sleep", data, (*sleepUnmarshal)(s))
}
31 changes: 8 additions & 23 deletions model/action_data_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

package model

import (
"bytes"
"encoding/json"
"fmt"
)

// ActionDataFilter used to filter action data results.
// +optional
// +optional
Expand All @@ -41,24 +35,15 @@ type ActionDataFilter struct {
ToStateData string `json:"toStateData,omitempty"`
}

type actionDataFilterForUnmarshal ActionDataFilter
type actionDataFilterUnmarshal ActionDataFilter

// UnmarshalJSON implements json.Unmarshaler
func (f *ActionDataFilter) UnmarshalJSON(data []byte) error {
data = bytes.TrimSpace(data)
if len(data) == 0 {
return fmt.Errorf("no bytes to unmarshal")
}

v := actionDataFilterForUnmarshal{
UseResults: true,
}
err := json.Unmarshal(data, &v)
if err != nil {
// TODO: replace the error message with correct type's name
return err
}
func (a *ActionDataFilter) UnmarshalJSON(data []byte) error {
a.ApplyDefault()
return unmarshalObject("actionDataFilter", data, (*actionDataFilterUnmarshal)(a))
}

*f = ActionDataFilter(v)
return nil
// ApplyDefault set the default values for Action Data Filter
func (a *ActionDataFilter) ApplyDefault() {
a.UseResults = true
}
2 changes: 1 addition & 1 deletion model/action_data_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestActionDataFilterUnmarshalJSON(t *testing.T) {
desp: "invalid json format",
data: `{"fromStateData": 1, "results": "2", "toStateData": "3"}`,
expect: ActionDataFilter{},
err: `json: cannot unmarshal number into Go struct field actionDataFilterForUnmarshal.fromStateData of type string`,
err: `actionDataFilter.fromStateData must be string`,
},
}

Expand Down
62 changes: 62 additions & 0 deletions model/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package model

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -92,3 +93,64 @@ func TestSleepValidate(t *testing.T) {
})
}
}

func TestFunctionRefUnmarshalJSON(t *testing.T) {
type testCase struct {
desp string
data string
expect FunctionRef
err string
}

testCases := []testCase{
{
desp: "invalid object refName",
data: `{"refName": 1}`,
expect: FunctionRef{},
err: "functionRef.refName must be string",
},
{
desp: "object with refName",
data: `{"refName": "function name"}`,
expect: FunctionRef{
RefName: "function name",
Invoke: InvokeKindSync,
},
err: ``,
},
{
desp: "object with refName and Invoke",
data: `{"refName": "function name", "invoke": "async"}`,
expect: FunctionRef{
RefName: "function name",
Invoke: InvokeKindAsync,
},
err: ``,
},
{
desp: "refName string",
data: `"function name"`,
expect: FunctionRef{
RefName: "function name",
Invoke: InvokeKindSync,
},
err: ``,
},
}

for _, tc := range testCases[:1] {
t.Run(tc.desp, func(t *testing.T) {
var v FunctionRef
err := json.Unmarshal([]byte(tc.data), &v)

if tc.err != "" {
assert.Error(t, err)
assert.Equal(t, tc.err, err.Error())
return
}

assert.NoError(t, err)
assert.Equal(t, tc.expect, v)
})
}
}
Loading