Skip to content

Commit e279256

Browse files
Fixes #25 - Sync with Spec 0.6 (#26)
* Fixes #25 - Sync with Spec 0.6 Signed-off-by: Ricardo Zanini <[email protected]> * Test Data Update / Schema Validation Signed-off-by: Ricardo Zanini <[email protected]> * Custom unmarshaling for bool/string representation of structs Signed-off-by: Ricardo Zanini <[email protected]> * Rebasing with previous PRs Signed-off-by: Ricardo Zanini <[email protected]> * Fixes #16 - Unmarshal Switch states Signed-off-by: Ricardo Zanini <[email protected]> * Documentation minor fixes Signed-off-by: Ricardo Zanini <[email protected]>
1 parent 9581d59 commit e279256

38 files changed

+1969
-2268
lines changed

Makefile

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.PHONY: gen-types
2-
gen-types:
3-
./hack/generate-spec-types.sh
4-
51
addheaders:
62
@command -v addlicense > /dev/null || go install -modfile=tools.mod -v github.com/google/addlicense
73
@addlicense -c "The Serverless Workflow Specification Authors" -l apache .

README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Current status of features implemented in the SDK is listed in the table below:
99
| Parse workflow JSON and YAML definitions | :heavy_check_mark: |
1010
| Programmatically build workflow definitions | :no_entry_sign: |
1111
| Validate workflow definitions (Schema) | :heavy_check_mark: |
12-
| Validate workflow definitions (Integrity) | :no_entry_sign: |
12+
| Validate workflow definitions (Integrity) | :heavy_check_mark: |
1313
| Generate workflow diagram (SVG) | :no_entry_sign: |
1414

1515
## Status
@@ -45,13 +45,12 @@ To transform such files into a Go data structure, use:
4545
package sw
4646

4747
import (
48-
49-
"github.com/serverlessworkflow/sdk-go/model"
50-
"github.com/serverlessworkflow/sdk-go/parser"
48+
"github.com/serverlessworkflow/sdk-go/model"
49+
"github.com/serverlessworkflow/sdk-go/parser"
5150
)
5251

5352
func ParseWorkflow(filePath string) (*model.Workflow, error) {
54-
workflow, err := parser.FromFile(filePath)
53+
workflow, err := parser.FromFile(filePath)
5554
if err != nil {
5655
return nil, err
5756
}

go.mod

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module github.com/serverlessworkflow/sdk-go
22

3-
go 1.14
3+
go 1.15
44

55
require (
66
github.com/stretchr/testify v1.6.1
7-
gopkg.in/yaml.v2 v2.3.0 // indirect
7+
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
8+
gopkg.in/go-playground/validator.v8 v8.18.2
9+
k8s.io/apimachinery v0.21.0
810
sigs.k8s.io/yaml v1.2.0
911
)

go.sum

+191-2
Large diffs are not rendered by default.

hack/generate-spec-types.sh

-65
This file was deleted.

hack/state_interface_impl.template

-23
This file was deleted.

hack/zz_generated.types_state_impl.go.template

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 The Serverless Workflow Specification Authors
1+
// Copyright 2021 The Serverless Workflow Specification Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,15 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT.
16-
1715
package model
1816

19-
// Serverless Workflow specification - common schema
20-
type Common map[string]interface{}
17+
// Common schema for Serverless Workflow specification
18+
type Common struct {
19+
// Metadata information
20+
Metadata Metadata `json:"metadata,omitempty"`
21+
}
2122

2223
// Metadata information
2324
type Metadata map[string]interface{}
24-
25-
// Metadata information
26-
type Metadata_1 map[string]interface{}

model/event.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
val "github.com/serverlessworkflow/sdk-go/validator"
19+
"gopkg.in/go-playground/validator.v8"
20+
"reflect"
21+
)
22+
23+
const (
24+
// EventKindConsumed ...
25+
EventKindConsumed EventKind = "consumed"
26+
// EventKindProduced ...
27+
EventKindProduced EventKind = "produced"
28+
)
29+
30+
func init() {
31+
val.GetValidator().RegisterStructValidation(EventStructLevelValidation, Event{})
32+
}
33+
34+
// EventStructLevelValidation custom validator for event kind consumed
35+
func EventStructLevelValidation(v *validator.Validate, structLevel *validator.StructLevel) {
36+
event := structLevel.CurrentStruct.Interface().(Event)
37+
38+
if event.Kind == EventKindConsumed && len(event.Type) == 0 {
39+
structLevel.ReportError(reflect.ValueOf(event.Type), "Type", "type", "reqtypeconsumed")
40+
}
41+
}
42+
43+
// EventKind ...
44+
type EventKind string
45+
46+
// Event ...
47+
type Event struct {
48+
Common
49+
// Unique event name
50+
Name string `json:"name" validate:"required"`
51+
// CloudEvent source
52+
Source string `json:"source,omitempty"`
53+
// CloudEvent type
54+
Type string `json:"type" validate:"required"`
55+
// Defines the CloudEvent as either 'consumed' or 'produced' by the workflow. Default is 'consumed'
56+
Kind EventKind `json:"kind,omitempty"`
57+
// CloudEvent correlation definitions
58+
Correlation []Correlation `json:"correlation,omitempty" validate:"omitempty,dive"`
59+
}
60+
61+
// Correlation ...
62+
type Correlation struct {
63+
// CloudEvent Extension Context Attribute name
64+
ContextAttributeName string `json:"contextAttributeName" validate:"required"`
65+
// CloudEvent Extension Context Attribute value
66+
ContextAttributeValue string `json:"contextAttributeValue,omitempty"`
67+
}
68+
69+
// EventRef ...
70+
type EventRef struct {
71+
// Reference to the unique name of a 'produced' event definition
72+
TriggerEventRef string `json:"triggerEventRef" validate:"required"`
73+
// Reference to the unique name of a 'consumed' event definition
74+
ResultEventRef string `json:"resultEventRef" validate:"required"`
75+
// TODO: create StringOrMap structure
76+
// If string type, an expression which selects parts of the states data output to become the data (payload) of the event referenced by 'triggerEventRef'. If object type, a custom object to become the data (payload) of the event referenced by 'triggerEventRef'.
77+
Data interface{} `json:"data,omitempty"`
78+
// Add additional extension context attributes to the produced event
79+
ContextAttributes map[string]interface{} `json:"contextAttributes,omitempty"`
80+
}

model/function.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 "encoding/json"
18+
19+
const (
20+
// FunctionTypeREST ...
21+
FunctionTypeREST FunctionType = "rest"
22+
// FunctionTypeRPC ...
23+
FunctionTypeRPC FunctionType = "rpc"
24+
// FunctionTypeExpression ...
25+
FunctionTypeExpression FunctionType = "expression"
26+
)
27+
28+
// FunctionType ...
29+
type FunctionType string
30+
31+
// Function ...
32+
type Function struct {
33+
Common
34+
// Unique function name
35+
Name string `json:"name" validate:"required"`
36+
// If type is `rest`, <path_to_openapi_definition>#<operation_id>. If type is `rpc`, <path_to_grpc_proto_file>#<service_name>#<service_method>. If type is `expression`, defines the workflow expression.
37+
Operation string `json:"operation" validate:"required"`
38+
// Defines the function type. Is either `rest`, `rpc` or `expression`. Default is `rest`
39+
Type FunctionType `json:"type,omitempty"`
40+
}
41+
42+
// FunctionRef ...
43+
type FunctionRef struct {
44+
// Name of the referenced function
45+
RefName string `json:"refName" validate:"required"`
46+
// Function arguments
47+
Arguments map[string]interface{} `json:"arguments,omitempty"`
48+
}
49+
50+
// UnmarshalJSON ...
51+
func (f *FunctionRef) UnmarshalJSON(data []byte) error {
52+
funcRef := make(map[string]interface{})
53+
if err := json.Unmarshal(data, &funcRef); err != nil {
54+
f.RefName, err = unmarshalString(data)
55+
if err != nil {
56+
return err
57+
}
58+
return nil
59+
}
60+
61+
f.RefName = requiresNotNilOrEmpty("refName")
62+
if _, found := funcRef["arguments"]; found {
63+
f.Arguments = funcRef["arguments"].(map[string]interface{})
64+
}
65+
66+
return nil
67+
}

model/retry.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
"k8s.io/apimachinery/pkg/util/intstr"
19+
)
20+
21+
// Retry ...
22+
type Retry struct {
23+
// Unique retry strategy name
24+
Name string `json:"name" validate:"required"`
25+
// Time delay between retry attempts (ISO 8601 duration format)
26+
Delay string `json:"delay,omitempty"`
27+
// Maximum time delay between retry attempts (ISO 8601 duration format)
28+
MaxDelay string `json:"maxDelay,omitempty"`
29+
// Static value by which the delay increases during each attempt (ISO 8601 time format)
30+
Increment string `json:"increment,omitempty"`
31+
// Numeric value, if specified the delay between retries is multiplied by this value.
32+
Multiplier intstr.IntOrString `json:"multiplier,omitempty" validate:"omitempty,min=0"`
33+
// Maximum number of retry attempts.
34+
MaxAttempts intstr.IntOrString `json:"maxAttempts" validate:"required,min=0"`
35+
// If float type, maximum amount of random time added or subtracted from the delay between each retry relative to total delay (between 0 and 1). If string type, absolute maximum amount of random time added or subtracted from the delay between each retry (ISO 8601 duration format)
36+
Jitter intstr.IntOrString `json:"jitter,omitempty" validate:"omitempty,min=0,max=1"`
37+
}

0 commit comments

Comments
 (0)