Skip to content

Commit bb8e1d9

Browse files
authored
feat(114): support IncludePath option to define directory for non-absolute import files (#130)
Signed-off-by: lsytj0413 <[email protected]> Signed-off-by: lsytj0413 <[email protected]>
1 parent 7b28917 commit bb8e1d9

File tree

6 files changed

+96
-4
lines changed

6 files changed

+96
-4
lines changed

model/util.go

+41-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"path/filepath"
2424
"strings"
25+
"sync/atomic"
2526

2627
"sigs.k8s.io/yaml"
2728
)
@@ -44,11 +45,20 @@ func getBytesFromFile(s string) (b []byte, err error) {
4445
}
4546
return buf.Bytes(), nil
4647
}
47-
if strings.HasPrefix(s, prefix) {
48-
s = strings.TrimPrefix(s, prefix)
49-
} else if s, err = filepath.Abs(s); err != nil {
50-
return nil, err
48+
s = strings.TrimPrefix(s, prefix)
49+
50+
if !filepath.IsAbs(s) {
51+
// The import file is an non-absolute path, we join it with include path
52+
// TODO: if the file didn't find in any include path, we should report an error
53+
for _, p := range IncludePaths() {
54+
sn := filepath.Join(p, s)
55+
if _, err := os.Stat(sn); err == nil {
56+
s = sn
57+
break
58+
}
59+
}
5160
}
61+
5262
if b, err = os.ReadFile(filepath.Clean(s)); err != nil {
5363
return nil, err
5464
}
@@ -104,3 +114,30 @@ func unmarshalFile(data []byte) (b []byte, err error) {
104114
}
105115
return file, nil
106116
}
117+
118+
var defaultIncludePaths atomic.Value
119+
120+
func init() {
121+
wd, err := os.Getwd()
122+
if err != nil {
123+
panic(err)
124+
}
125+
126+
SetIncludePaths([]string{wd})
127+
}
128+
129+
// IncludePaths will return the search path for non-absolute import file
130+
func IncludePaths() []string {
131+
return defaultIncludePaths.Load().([]string)
132+
}
133+
134+
// SetIncludePaths will update the search path for non-absolute import file
135+
func SetIncludePaths(paths []string) {
136+
for _, path := range paths {
137+
if !filepath.IsAbs(path) {
138+
panic(fmt.Errorf("%s must be an absolute file path", path))
139+
}
140+
}
141+
142+
defaultIncludePaths.Store(paths)
143+
}

model/util_benchmark_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 model
16+
17+
import (
18+
"fmt"
19+
"testing"
20+
)
21+
22+
func Benchmark_IncludePaths_Parallel(b *testing.B) {
23+
b.RunParallel(func(p *testing.PB) {
24+
i := 0
25+
for p.Next() {
26+
IncludePaths()
27+
SetIncludePaths([]string{fmt.Sprintf("%v", i)})
28+
i++
29+
}
30+
})
31+
}

model/util_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,23 @@
1313
// limitations under the License.
1414

1515
package model
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
)
22+
23+
func TestIncludePaths(t *testing.T) {
24+
assert.NotNil(t, IncludePaths())
25+
assert.True(t, len(IncludePaths()) > 0)
26+
27+
// update include paths
28+
paths := []string{"/root", "/path"}
29+
SetIncludePaths(paths)
30+
assert.Equal(t, IncludePaths(), paths)
31+
32+
assert.PanicsWithError(t, "1 must be an absolute file path", assert.PanicTestFunc(func() {
33+
SetIncludePaths([]string{"1"})
34+
}))
35+
}

parser/parser_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ import (
2424
"github.com/stretchr/testify/assert"
2525

2626
"github.com/serverlessworkflow/sdk-go/v2/model"
27+
"github.com/serverlessworkflow/sdk-go/v2/test"
2728
)
2829

2930
func TestBasicValidation(t *testing.T) {
3031
rootPath := "./testdata/workflows"
3132
files, err := os.ReadDir(rootPath)
3233
assert.NoError(t, err)
34+
35+
model.SetIncludePaths(append(model.IncludePaths(), filepath.Join(test.CurrentProjectPath(), "./parser/testdata")))
36+
3337
for _, file := range files {
3438
if !file.IsDir() {
3539
workflow, err := FromFile(filepath.Join(rootPath, file.Name()))
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)