-
Notifications
You must be signed in to change notification settings - Fork 2
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
data.json
plugin
#30
Merged
Merged
data.json
plugin
#30
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package plugin | ||
package plugininterface | ||
|
||
import "github.com/Masterminds/semver/v3" | ||
|
||
|
This file contains 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,71 @@ | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
type testFS struct { | ||
FS fs.FS | ||
path string | ||
} | ||
|
||
func makeTestFS(tb testing.TB) testFS { | ||
tb.Helper() | ||
|
||
path, err := filepath.EvalSymlinks(tb.TempDir()) | ||
if err != nil { | ||
tb.Fatalf("failed to create testFS: %s", err) | ||
} | ||
|
||
path = filepath.ToSlash(path) | ||
|
||
tb.Logf("creating testFS at %s", path) | ||
return testFS{ | ||
FS: os.DirFS(path), | ||
path: path, | ||
} | ||
} | ||
|
||
func (t testFS) Open(name string) (fs.File, error) { | ||
return t.FS.Open(filepath.ToSlash(name)) | ||
} | ||
|
||
func (t testFS) Path() string { | ||
return t.path | ||
} | ||
|
||
func (t testFS) WriteFile(name string, data []byte, perm os.FileMode) error { | ||
name = filepath.ToSlash(name) | ||
if filepath.IsAbs(name) { | ||
if strings.HasPrefix(name, t.path) { | ||
return os.WriteFile(name, data, perm) | ||
} | ||
return fmt.Errorf("path is outside test fs root folder") | ||
} | ||
return os.WriteFile(filepath.ToSlash(filepath.Join(t.path, name)), data, perm) | ||
} | ||
|
||
func (t testFS) MkdirAll(path string, perm os.FileMode) error { | ||
path = filepath.ToSlash(path) | ||
if filepath.IsAbs(path) { | ||
if strings.HasPrefix(path, t.path) { | ||
return os.MkdirAll(path, perm) | ||
} | ||
return fmt.Errorf("path is outside test fs root folder") | ||
} | ||
return os.MkdirAll(filepath.ToSlash(filepath.Join(t.path, path)), perm) | ||
} | ||
|
||
func testJSON(m any) json.RawMessage { | ||
contents, err := json.Marshal(m) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return contents | ||
} |
This file contains 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,66 @@ | ||
package json | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"github.com/blackstork-io/fabric/plugininterface/v1" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hcldec" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
var Version = semver.MustParse("0.1.0") | ||
|
||
type Plugin struct{} | ||
|
||
func (Plugin) GetPlugins() []plugininterface.Plugin { | ||
return []plugininterface.Plugin{ | ||
{ | ||
Namespace: "blackstork", | ||
Kind: "data", | ||
Name: "json", | ||
Version: plugininterface.Version(*Version), | ||
ConfigSpec: nil, | ||
InvocationSpec: &hcldec.ObjectSpec{ | ||
"glob": &hcldec.AttrSpec{ | ||
Name: "glob", | ||
Type: cty.String, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (Plugin) Call(args plugininterface.Args) plugininterface.Result { | ||
glob := args.Args.GetAttr("glob").AsString() | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
return plugininterface.Result{ | ||
Diags: hcl.Diagnostics{{ | ||
Severity: hcl.DiagError, | ||
Summary: "Failed to get current working directory", | ||
Detail: err.Error(), | ||
}}, | ||
} | ||
} | ||
filesystem := os.DirFS(wd) | ||
docs, err := readFS(filesystem, glob) | ||
if err != nil { | ||
return plugininterface.Result{ | ||
Diags: hcl.Diagnostics{{ | ||
Severity: hcl.DiagError, | ||
Summary: "Failed to read json files", | ||
Detail: err.Error(), | ||
}}, | ||
} | ||
} | ||
data := make([]any, len(docs)) | ||
for i, doc := range docs { | ||
data[i] = doc.Map() | ||
} | ||
return plugininterface.Result{ | ||
Result: data, | ||
} | ||
} |
This file contains 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,105 @@ | ||
package json | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/blackstork-io/fabric/plugininterface/v1" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func TestPlugin_GetPlugins(t *testing.T) { | ||
plugin := Plugin{} | ||
plugins := plugin.GetPlugins() | ||
|
||
if len(plugins) != 1 { | ||
t.Fatalf("expected 2 plugins, got %d", len(plugins)) | ||
} | ||
got := plugins[0] | ||
assert.Equal(t, "json", got.Name) | ||
assert.Equal(t, "data", got.Kind) | ||
assert.Equal(t, "blackstork", got.Namespace) | ||
assert.Equal(t, Version.String(), got.Version.Cast().String()) | ||
assert.Nil(t, got.ConfigSpec) | ||
assert.NotNil(t, got.InvocationSpec) | ||
} | ||
|
||
func TestPlugin_Call(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
glob string | ||
expected plugininterface.Result | ||
}{ | ||
{ | ||
name: "empty_list", | ||
glob: "unknown_dir/*.json", | ||
expected: plugininterface.Result{ | ||
Result: []any{}, | ||
}, | ||
}, | ||
{ | ||
name: "one_file", | ||
glob: "testdata/a.json", | ||
expected: plugininterface.Result{ | ||
Result: []any{ | ||
map[string]any{ | ||
"filename": "testdata/a.json", | ||
"contents": map[string]any{ | ||
"property_for": "a.json", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "dir", | ||
glob: "testdata/dir/*.json", | ||
expected: plugininterface.Result{ | ||
Result: []any{ | ||
map[string]any{ | ||
"filename": "testdata/dir/b.json", | ||
"contents": []any{ | ||
map[string]any{ | ||
"id": float64(1), | ||
"property_for": "dir/b.json", | ||
}, | ||
map[string]any{ | ||
"id": float64(2), | ||
"property_for": "dir/b.json", | ||
}, | ||
}, | ||
}, | ||
map[string]any{ | ||
"filename": "testdata/dir/c.json", | ||
"contents": []any{ | ||
map[string]any{ | ||
"id": float64(3), | ||
"property_for": "dir/c.json", | ||
}, | ||
map[string]any{ | ||
"id": float64(4), | ||
"property_for": "dir/c.json", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
plugin := Plugin{} | ||
args := plugininterface.Args{ | ||
Kind: "json", | ||
Name: "json", | ||
Args: cty.ObjectVal(map[string]cty.Value{ | ||
"glob": cty.StringVal(tc.glob), | ||
}), | ||
} | ||
got := plugin.Call(args) | ||
assert.Equal(t, tc.expected, got) | ||
}) | ||
} | ||
|
||
} |
This file contains 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,47 @@ | ||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
"io/fs" | ||
) | ||
|
||
// JSONDocument represents a JSON document that was read from the filesystem | ||
type JSONDocument struct { | ||
Filename string `json:"filename"` | ||
Contents json.RawMessage `json:"contents"` | ||
} | ||
|
||
func (doc JSONDocument) Map() map[string]any { | ||
var result any | ||
_ = json.Unmarshal(doc.Contents, &result) | ||
return map[string]any{ | ||
"filename": doc.Filename, | ||
"contents": result, | ||
} | ||
} | ||
|
||
// readFS reads all JSON documents from the filesystem that match the given glob pattern | ||
// The pattern is relative to the root of the filesystem | ||
func readFS(filesystem fs.FS, pattern string) ([]JSONDocument, error) { | ||
matchers, err := fs.Glob(filesystem, pattern) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result := []JSONDocument{} | ||
for _, matcher := range matchers { | ||
file, err := filesystem.Open(matcher) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var contents json.RawMessage | ||
err = json.NewDecoder(file).Decode(&contents) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result = append(result, JSONDocument{ | ||
Filename: matcher, | ||
Contents: contents, | ||
}) | ||
} | ||
return result, nil | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should that be
expected 1 plugin
?