Skip to content

Commit 3c3f359

Browse files
committed
review additions
Signed-off-by: Spolti <[email protected]>
1 parent 54ba2c4 commit 3c3f359

File tree

11 files changed

+112
-25
lines changed

11 files changed

+112
-25
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ lint:
1414

1515
.PHONY: test
1616
coverage="false"
17+
1718
test: deepcopy buildergen
1819
make lint
1920
@go test ./...

builder/builder_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package builder
1717
import (
1818
"testing"
1919

20+
"github.com/pkg/errors"
2021
"github.com/stretchr/testify/assert"
2122

2223
"github.com/serverlessworkflow/sdk-go/v2/model"
@@ -58,7 +59,13 @@ func TestValidate(t *testing.T) {
5859
state2.End().Terminate(true)
5960
err = Validate(state2.Build())
6061
if assert.Error(t, err) {
61-
assert.Equal(t, "state.name is required", err.(val.WorkflowErrors)[0].Error())
62+
var workflowErrors val.WorkflowErrors
63+
if errors.As(err, &workflowErrors) {
64+
assert.Equal(t, "state.name is required", workflowErrors[0].Error())
65+
} else {
66+
// Handle other error types if necessary
67+
t.Errorf("Unexpected error: %v", err)
68+
}
6269
}
6370
}
6471

model/workflow.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package model
1616

1717
import (
18-
"bytes"
1918
"encoding/json"
19+
"regexp"
2020

2121
"github.com/serverlessworkflow/sdk-go/v2/util"
2222
)
@@ -122,7 +122,7 @@ type BaseWorkflow struct {
122122
// qualities.
123123
// +optional
124124
Annotations []string `json:"annotations,omitempty"`
125-
// DataInputSchema URI of the JSON Schema used to validate the workflow data input
125+
// DataInputSchema URI or Object of the JSON Schema used to validate the workflow data input
126126
// +optional
127127
DataInputSchema *DataInputSchema `json:"dataInputSchema,omitempty"`
128128
// Serverless Workflow schema version
@@ -522,12 +522,32 @@ type dataInputSchemaUnmarshal DataInputSchema
522522
// UnmarshalJSON implements json.Unmarshaler
523523
func (d *DataInputSchema) UnmarshalJSON(data []byte) error {
524524
d.ApplyDefault()
525-
if data[0] == '"' && len(data) > 0 {
526-
replaced := bytes.Replace(data, []byte(`"`), []byte(``), -1)
527-
repp := FromString(string(replaced))
528-
d.Schema = &repp
529-
} else {
530-
return util.UnmarshalObject("dataInputSchema", data, (*dataInputSchemaUnmarshal)(d))
525+
err := util.UnmarshalObject("dataInputSchema", data, (*dataInputSchemaUnmarshal)(d))
526+
if err != nil {
527+
528+
return err
529+
}
530+
531+
if d.Schema != nil && d.Schema.Type == String {
532+
// Define the regex pattern to match the prefixes
533+
pattern := `^(http|https|file)`
534+
regex := regexp.MustCompile(pattern)
535+
// if it is not external, treat as JSON object
536+
if !regex.MatchString(d.Schema.StringValue) {
537+
point := FromString(d.Schema.StringValue)
538+
d.Schema = &point
539+
return nil
540+
}
541+
542+
data, err := util.LoadExternalResource(d.Schema.StringValue)
543+
if err != nil {
544+
return err
545+
}
546+
547+
er := util.UnmarshalObject("schema", data, &d.Schema)
548+
// clean the string value to avoid the json URI being appended
549+
d.Schema.StringValue = ""
550+
return er
531551
}
532552
return nil
533553
}

model/workflow_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ func TestDataInputSchemaUnmarshalJSON(t *testing.T) {
516516
Schema: &schemaName,
517517
FailOnValidationErrors: true,
518518
},
519-
err: ``,
519+
err: `dataInputSchema must be string or object`,
520520
},
521521
{
522522
desp: `object success`,

model/workflow_validator_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ func TestDataInputSchemaStructLevelValidation(t *testing.T) {
449449
},
450450
}
451451

452+
//fmt.Printf("%+v", testCases[0].Model)
452453
StructLevelValidationCtx(t, testCases)
453454
}
454455

model/zz_generated.buildergen.go

Lines changed: 36 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/parser_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,12 @@ func TestFromFile(t *testing.T) {
583583
}, {
584584
"./testdata/workflows/dataInputSchemaValidation.yaml", func(t *testing.T, w *model.Workflow) {
585585
assert.NotNil(t, w.DataInputSchema)
586-
expected := model.FromString("sample schema")
587-
assert.Equal(t, &expected, w.DataInputSchema.Schema)
586+
expected := model.DataInputSchema{}
587+
data, err := util.LoadExternalResource("file://testdata/datainputschema.json")
588+
err1 := util.UnmarshalObject("schema", data, &expected.Schema)
589+
assert.Nil(t, err)
590+
assert.Nil(t, err1)
591+
assert.Equal(t, expected.Schema, w.DataInputSchema.Schema)
588592
assert.Equal(t, false, w.DataInputSchema.FailOnValidationErrors)
589593
},
590594
}, {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"title": "Hello World Schema",
3+
"properties": {
4+
"person": {
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"type": "string"
9+
}
10+
},
11+
"required": [
12+
"name"
13+
]
14+
}
15+
}
16+
}

parser/testdata/workflows/dataInputSchemaValidation.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ specVersion: '0.8'
1818
start: Start
1919
dataInputSchema:
2020
failOnValidationErrors: false
21-
schema: "sample schema"
21+
schema: "file://testdata/datainputschema.json"
2222
states:
2323
- name: Start
2424
type: inject

util/unmarshal.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ func (e *UnmarshalError) Error() string {
7272
func (e *UnmarshalError) unmarshalMessageError(err *json.UnmarshalTypeError) string {
7373
if err.Struct == "" && err.Field == "" {
7474
primitiveTypeName := e.primitiveType.String()
75+
76+
// in some cases the e.primitiveType might be invalid, one of the reasons is because it is nil
77+
// default to string in that case
78+
if e.primitiveType == reflect.Invalid {
79+
primitiveTypeName = "string"
80+
}
81+
7582
var objectTypeName string
7683
if e.objectType != reflect.Invalid {
7784
switch e.objectType {
@@ -107,7 +114,7 @@ func (e *UnmarshalError) unmarshalMessageError(err *json.UnmarshalTypeError) str
107114
return err.Error()
108115
}
109116

110-
func loadExternalResource(url string) (b []byte, err error) {
117+
func LoadExternalResource(url string) (b []byte, err error) {
111118
index := strings.Index(url, "://")
112119
if index == -1 {
113120
b, err = getBytesFromFile(url)
@@ -199,7 +206,7 @@ func UnmarshalObjectOrFile[U any](parameterName string, data []byte, valObject *
199206

200207
// Assumes that the value inside `data` is a path to a known location.
201208
// Returns the content of the file or a not nil error reference.
202-
data, err = loadExternalResource(valString)
209+
data, err = LoadExternalResource(valString)
203210
if err != nil {
204211
return err
205212
}

0 commit comments

Comments
 (0)