|
| 1 | +// Copyright 2022 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 main |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "errors" |
| 20 | + "log" |
| 21 | + "os" |
| 22 | + "path" |
| 23 | + "path/filepath" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "gopkg.in/yaml.v3" |
| 27 | + |
| 28 | + "github.com/serverlessworkflow/sdk-go/v2/test" |
| 29 | +) |
| 30 | + |
| 31 | +func convert(i interface{}) interface{} { |
| 32 | + switch x := i.(type) { |
| 33 | + case map[interface{}]interface{}: |
| 34 | + m2 := map[string]interface{}{} |
| 35 | + for k, v := range x { |
| 36 | + m2[k.(string)] = convert(v) |
| 37 | + } |
| 38 | + return m2 |
| 39 | + case []interface{}: |
| 40 | + for i, v := range x { |
| 41 | + x[i] = convert(v) |
| 42 | + } |
| 43 | + } |
| 44 | + return i |
| 45 | +} |
| 46 | + |
| 47 | +func transform( |
| 48 | + files []string, |
| 49 | + srcFormat string, |
| 50 | + destFormat string, |
| 51 | + unmarshal func(data []byte, out interface{}) error, |
| 52 | + marshal func(in interface{}) ([]byte, error), |
| 53 | +) { |
| 54 | + for _, srcFile := range files { |
| 55 | + if !strings.HasSuffix(srcFile, srcFormat) { |
| 56 | + log.Printf("%s is not %s format, skip it", srcFile, srcFormat) |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + destFile := srcFile[0:len(srcFile)-len(srcFormat)] + destFormat |
| 61 | + if _, err := os.Stat(destFile); err == nil { |
| 62 | + log.Printf("ERR: the target file %v exists, skip it", destFile) |
| 63 | + continue |
| 64 | + } else if !errors.Is(err, os.ErrNotExist) { |
| 65 | + log.Printf("ERR: stat target file %v, %v, skip it", destFile, err) |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + srcData, err := os.ReadFile(filepath.Clean(srcFile)) |
| 70 | + if err != nil { |
| 71 | + log.Printf("ERR: cannot read file %v, %v, skip it", srcFile, err) |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + var srcObj interface{} |
| 76 | + err = unmarshal(srcData, &srcObj) |
| 77 | + if err != nil { |
| 78 | + log.Printf("ERR: cannot unmarshal file %v to %s, %v, skip it", srcFile, srcFormat, err) |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + destObj := convert(srcObj) |
| 83 | + destData, err := marshal(destObj) |
| 84 | + if err != nil { |
| 85 | + log.Printf("ERR: cannot marshal fild %v data to %v, %v, skip it", srcFile, destFormat, err) |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + err = os.WriteFile(destFile, destData, 0600) |
| 90 | + if err != nil { |
| 91 | + log.Printf("ERR: cannot write to file %v, %v, skip it", destFile, err) |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + log.Printf("convert %v to %v done", srcFile, destFile) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func main() { |
| 100 | + // TODO: make this as argument |
| 101 | + dir := path.Join(test.CurrentProjectPath(), "parser", "testdata", "workflows", "urifiles") |
| 102 | + dirEntries, err := os.ReadDir(dir) |
| 103 | + if err != nil { |
| 104 | + panic(err) |
| 105 | + } |
| 106 | + |
| 107 | + files := make([]string, 0, len(dirEntries)) |
| 108 | + for _, entry := range dirEntries { |
| 109 | + if entry.IsDir() { |
| 110 | + log.Printf("%s is directory, skip it", entry.Name()) |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + files = append(files, path.Join(dir, entry.Name())) |
| 115 | + } |
| 116 | + |
| 117 | + log.Printf("found %v files", len(files)) |
| 118 | + |
| 119 | + // First, convert all json format files to yaml |
| 120 | + log.Printf("start to convert all json format files to yaml format") |
| 121 | + transform(files, ".json", ".yaml", json.Unmarshal, yaml.Marshal) |
| 122 | + |
| 123 | + // Second, convert all yaml format files to json |
| 124 | + log.Printf("start to convert all yaml format files to json format") |
| 125 | + transform(files, ".yaml", ".json", yaml.Unmarshal, func(in interface{}) ([]byte, error) { |
| 126 | + return json.MarshalIndent(in, "", " ") |
| 127 | + }) |
| 128 | +} |
0 commit comments