Skip to content

Commit

Permalink
Correctly load jsonnet files with multiple objects (#58)
Browse files Browse the repository at this point in the history
* Correctly load jsonnet files with multiple objects

* Add JSONNET section to Readme

* Add explanation for jsonnetparser's behaviour
  • Loading branch information
wurbanski authored Apr 21, 2020
1 parent ea6ac38 commit 7e7b96f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 24 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ Difference between a.json and b.json:
+ "true"
```

## Using JSONNET

Files that have a `.jsonnet` file extention will be evaluated as JSONNET files. Output of each file should be either a single object or an array of objects.

## License

Floodgate is licensed under Apache 2.0 License, following other Spinnaker's components.
Expand Down
2 changes: 1 addition & 1 deletion parser/fileloader/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ package fileloader
//
// SupportedFileExtensions returns a slice of strings representing supported file format extensions
type FileLoader interface {
LoadFile(filePath string) (map[string]interface{}, error)
LoadFile(filePath string) ([]map[string]interface{}, error)
SupportedFileExtensions() []string
}
4 changes: 2 additions & 2 deletions parser/fileloader/jsonloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func NewJSONLoader() *JSONLoader {
type JSONLoader struct{}

// LoadFile load file
func (jl *JSONLoader) LoadFile(filePath string) (map[string]interface{}, error) {
func (jl *JSONLoader) LoadFile(filePath string) ([]map[string]interface{}, error) {
inputFile, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
Expand All @@ -23,7 +23,7 @@ func (jl *JSONLoader) LoadFile(filePath string) (map[string]interface{}, error)
if err := json.Unmarshal(inputFile, &output); err != nil {
return nil, err
}
return output, nil
return []map[string]interface{}{output}, nil
}

// SupportedFileExtensions get supported file extensions
Expand Down
6 changes: 3 additions & 3 deletions parser/fileloader/jsonloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestJSONLoader_LoadFile(t *testing.T) {
tests := []struct {
name string
args args
want map[string]interface{}
want []map[string]interface{}
wantErr bool
}{
{
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestJSONLoader_SupportedFileExtensions(t *testing.T) {
}
}

var testJSONFIle = map[string]interface{}{
var testJSONFIle = []map[string]interface{}{{
"name": "testjson",
"variable": false,
}
}}
21 changes: 16 additions & 5 deletions parser/fileloader/jsonnetloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,28 @@ type JsonnetLoader struct {
}

// LoadFile load file
func (jl *JsonnetLoader) LoadFile(filePath string) (map[string]interface{}, error) {
func (jl *JsonnetLoader) LoadFile(filePath string) ([]map[string]interface{}, error) {
inputFile, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
evaluatedSnipped, err := jl.EvaluateSnippet(filePath, string(inputFile))
// EvaluateSnippetStream fails if generated output does not fit into a slice...
snippetStream, err := jl.EvaluateSnippetStream(filePath, string(inputFile))
if err != nil {
return nil, err
// ...at which point we evaluate it as a single object and let further processing take care
evaluatedSingleSnippet, err := jl.EvaluateSnippet(filePath, string(inputFile))
if err != nil {
return nil, err
}
// wrap the single object in slice to keep interface intact
snippetStream = []string{evaluatedSingleSnippet}
}
var output []map[string]interface{}
for i := range snippetStream {
var partial map[string]interface{}
json.Unmarshal([]byte(snippetStream[i]), &partial)
output = append(output, partial)
}
var output map[string]interface{}
json.Unmarshal([]byte(evaluatedSnipped), &output)
return output, nil
}

Expand Down
6 changes: 3 additions & 3 deletions parser/fileloader/jsonnetloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestJsonnetLoader_LoadFile(t *testing.T) {
name string
fields fields
args args
want map[string]interface{}
want []map[string]interface{}
wantErr bool
}{
{
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestJsonnetLoader_SupportedFileExtensions(t *testing.T) {
}
}

var testJsonnetFile = map[string]interface{}{
var testJsonnetFile = []map[string]interface{}{{
"name": "testjsonnet",
"variable": false,
}
}}
4 changes: 2 additions & 2 deletions parser/fileloader/yamlloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewYAMLLoader() *YAMLLoader {
type YAMLLoader struct{}

// LoadFile load file
func (yl *YAMLLoader) LoadFile(filePath string) (map[string]interface{}, error) {
func (yl *YAMLLoader) LoadFile(filePath string) ([]map[string]interface{}, error) {
inputFile, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
Expand All @@ -24,7 +24,7 @@ func (yl *YAMLLoader) LoadFile(filePath string) (map[string]interface{}, error)
if err := yaml.Unmarshal(inputFile, &output); err != nil {
return nil, err
}
return output, nil
return []map[string]interface{}{output}, nil
}

// SupportedFileExtensions get supported file extensions
Expand Down
10 changes: 5 additions & 5 deletions parser/fileloader/yamlloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestYAMLLoader_LoadFile(t *testing.T) {
tests := []struct {
name string
args args
want map[string]interface{}
want []map[string]interface{}
wantErr bool
}{
{
Expand Down Expand Up @@ -67,12 +67,12 @@ func TestYAMLLoader_SupportedFileExtensions(t *testing.T) {
}
}

var testYAMLFile = map[string]interface{}{
var testYAMLFile = []map[string]interface{}{{
"name": "testyaml",
"variable": false,
}
}}

var testYMLFile = map[string]interface{}{
var testYMLFile = []map[string]interface{}{{
"name": "testyml",
"variable": false,
}
}}
5 changes: 2 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (p *Parser) loadFilesFromDirectory(entrypoint string) ([]map[string]interfa
log.Warn(f.Name(), "not loaded due to", err)
return nil
}
objects = append(objects, obj)
objects = append(objects, obj...)
log.Printf("%s loaded", f.Name())
return nil
})
Expand All @@ -121,8 +121,7 @@ func (p *Parser) parseObjects(objects []map[string]interface{}) (*ParsedResource
parsedResources.PipelineTemplates = append(parsedResources.PipelineTemplates, object)
continue
}
if _, ok := object["providerSettings"]; ok {

if _, ok := object["email"]; ok {
parsedResources.Applications = append(parsedResources.Applications, object)
continue
}
Expand Down

0 comments on commit 7e7b96f

Please sign in to comment.