-
Notifications
You must be signed in to change notification settings - Fork 44
fix(103): support refs to yaml format file #105
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2022 The Serverless Workflow Specification Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"log" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/serverlessworkflow/sdk-go/v2/test" | ||
) | ||
|
||
func convert(i interface{}) interface{} { | ||
switch x := i.(type) { | ||
case map[interface{}]interface{}: | ||
m2 := map[string]interface{}{} | ||
for k, v := range x { | ||
m2[k.(string)] = convert(v) | ||
} | ||
return m2 | ||
case []interface{}: | ||
for i, v := range x { | ||
x[i] = convert(v) | ||
} | ||
} | ||
return i | ||
} | ||
|
||
func transform( | ||
files []string, | ||
srcFormat string, | ||
destFormat string, | ||
unmarshal func(data []byte, out interface{}) error, | ||
marshal func(in interface{}) ([]byte, error), | ||
) { | ||
for _, srcFile := range files { | ||
if !strings.HasSuffix(srcFile, srcFormat) { | ||
log.Printf("%s is not %s format, skip it", srcFile, srcFormat) | ||
continue | ||
} | ||
|
||
destFile := srcFile[0:len(srcFile)-len(srcFormat)] + destFormat | ||
if _, err := os.Stat(destFile); err == nil { | ||
log.Printf("ERR: the target file %v exists, skip it", destFile) | ||
continue | ||
} else if !errors.Is(err, os.ErrNotExist) { | ||
log.Printf("ERR: stat target file %v, %v, skip it", destFile, err) | ||
continue | ||
} | ||
|
||
srcData, err := os.ReadFile(filepath.Clean(srcFile)) | ||
if err != nil { | ||
log.Printf("ERR: cannot read file %v, %v, skip it", srcFile, err) | ||
continue | ||
} | ||
|
||
var srcObj interface{} | ||
err = unmarshal(srcData, &srcObj) | ||
if err != nil { | ||
log.Printf("ERR: cannot unmarshal file %v to %s, %v, skip it", srcFile, srcFormat, err) | ||
continue | ||
} | ||
|
||
destObj := convert(srcObj) | ||
destData, err := marshal(destObj) | ||
if err != nil { | ||
log.Printf("ERR: cannot marshal fild %v data to %v, %v, skip it", srcFile, destFormat, err) | ||
continue | ||
} | ||
|
||
err = os.WriteFile(destFile, destData, 0600) | ||
if err != nil { | ||
log.Printf("ERR: cannot write to file %v, %v, skip it", destFile, err) | ||
continue | ||
} | ||
|
||
log.Printf("convert %v to %v done", srcFile, destFile) | ||
} | ||
} | ||
|
||
func main() { | ||
// TODO: make this as argument | ||
dir := path.Join(test.CurrentProjectPath(), "parser", "testdata", "workflows", "urifiles") | ||
dirEntries, err := os.ReadDir(dir) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
files := make([]string, 0, len(dirEntries)) | ||
for _, entry := range dirEntries { | ||
if entry.IsDir() { | ||
log.Printf("%s is directory, skip it", entry.Name()) | ||
continue | ||
} | ||
|
||
files = append(files, path.Join(dir, entry.Name())) | ||
} | ||
|
||
log.Printf("found %v files", len(files)) | ||
|
||
// First, convert all json format files to yaml | ||
log.Printf("start to convert all json format files to yaml format") | ||
transform(files, ".json", ".yaml", json.Unmarshal, yaml.Marshal) | ||
|
||
// Second, convert all yaml format files to json | ||
log.Printf("start to convert all yaml format files to json format") | ||
transform(files, ".yaml", ".json", yaml.Unmarshal, func(in interface{}) ([]byte, error) { | ||
return json.MarshalIndent(in, "", " ") | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
parser/testdata/workflows/applicationrequest-issue103.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
{ | ||
"id": "applicantrequest", | ||
"version": "1.0", | ||
"name": "Applicant Request Decision Workflow", | ||
"description": "Determine if applicant request is valid", | ||
"start": "CheckApplication", | ||
"specVersion": "0.7", | ||
"auth": "./testdata/workflows/urifiles/auth.yaml", | ||
"functions": [ | ||
{ | ||
"name": "sendRejectionEmailFunction", | ||
"operation": "http://myapis.org/applicationapi.json#emailRejection" | ||
} | ||
], | ||
"retries": [ | ||
{ | ||
"name": "TimeoutRetryStrategy", | ||
"delay": "PT1M", | ||
"maxAttempts": "5" | ||
} | ||
], | ||
"states": [ | ||
{ | ||
"name": "CheckApplication", | ||
"type": "switch", | ||
"dataConditions": [ | ||
{ | ||
"condition": "${ .applicants | .age >= 18 }", | ||
"transition": { | ||
"nextState": "StartApplication" | ||
} | ||
}, | ||
{ | ||
"condition": "${ .applicants | .age < 18 }", | ||
"transition": { | ||
"nextState": "RejectApplication" | ||
} | ||
} | ||
], | ||
"default": { | ||
"transition": { | ||
"nextState": "RejectApplication" | ||
} | ||
} | ||
}, | ||
{ | ||
"name": "StartApplication", | ||
"type": "operation", | ||
"actions": [ | ||
{ | ||
"subFlowRef": { | ||
"workflowId": "startApplicationWorkflowId" | ||
} | ||
} | ||
], | ||
"end": { | ||
"terminate": true | ||
} | ||
}, | ||
{ | ||
"name": "RejectApplication", | ||
"type": "operation", | ||
"actionMode": "sequential", | ||
"actions": [ | ||
{ | ||
"functionRef": { | ||
"refName": "sendRejectionEmailFunction", | ||
"parameters": { | ||
"applicant": "${ .applicant }" | ||
} | ||
} | ||
} | ||
], | ||
"end": { | ||
"terminate": true | ||
} | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2022 The Serverless Workflow Specification Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
- name: testAuth | ||
properties: | ||
token: test_token | ||
scheme: bearer | ||
- name: testAuth2 | ||
properties: | ||
password: test_pwd | ||
username: test_user | ||
scheme: basic |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2022 The Serverless Workflow Specification Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// CurrentProjectPath get the project root path | ||
func CurrentProjectPath() string { | ||
path := currentFilePath() | ||
|
||
ppath, err := filepath.Abs(filepath.Join(filepath.Dir(path), "../")) | ||
if err != nil { | ||
panic(errors.Wrapf(err, "Get current project path with %s failed", path)) | ||
} | ||
|
||
f, err := os.Stat(ppath) | ||
if err != nil { | ||
panic(errors.Wrapf(err, "Stat project path %v failed", ppath)) | ||
} | ||
|
||
if f.Mode()&os.ModeSymlink != 0 { | ||
fpath, err := os.Readlink(ppath) | ||
if err != nil { | ||
panic(errors.Wrapf(err, "Readlink from path %v failed", fpath)) | ||
} | ||
ppath = fpath | ||
} | ||
|
||
return ppath | ||
} | ||
|
||
func currentFilePath() string { | ||
_, file, _, _ := runtime.Caller(1) | ||
return file | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2022 The Serverless Workflow Specification Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCurrentProjectPath(t *testing.T) { | ||
t.Run("normal test", func(t *testing.T) { | ||
path := CurrentProjectPath() | ||
|
||
// NOTE: the '/code' path is used with code pipeline. | ||
// When code running in the pipeline, the codebase will copy to /home/code directory. | ||
assert.Regexp(t, "(/sdk-go$)|(/code$)", path) | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.