-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* dep update * Adding meta field to step model. * Using string to interface{} map for meta tag * missing packages * Convert to map[string]interface{} * Dep update * remove test case * Converting StepModel receivers to pointer types. * Dep update * Renaming function * fixes * dep update
- Loading branch information
Showing
36 changed files
with
5,119 additions
and
1,755 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,64 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// JSONMarshallable replaces map[interface{}]interface{} with map[string]string recursively | ||
// map[interface{}]interface{} is usually returned by parser go-yaml/v2 | ||
func JSONMarshallable(source map[string]interface{}) (map[string]interface{}, error) { | ||
target, err := recursiveJSONMarshallable(source) | ||
if err != nil { | ||
return nil, err | ||
} | ||
castedTarget, ok := target.(map[string]interface{}) | ||
if !ok { | ||
return nil, fmt.Errorf("could not cast to map[string]interface{}") | ||
} | ||
return castedTarget, nil | ||
} | ||
|
||
func recursiveJSONMarshallable(source interface{}) (interface{}, error) { | ||
if array, ok := source.([]interface{}); ok { | ||
var convertedArray []interface{} | ||
for _, element := range array { | ||
convertedValue, err := recursiveJSONMarshallable(element) | ||
if err != nil { | ||
return nil, err | ||
} | ||
convertedArray = append(convertedArray, convertedValue) | ||
} | ||
return convertedArray, nil | ||
} | ||
|
||
if interfaceToInterfaceMap, ok := source.(map[interface{}]interface{}); ok { | ||
target := map[string]interface{}{} | ||
for key, value := range interfaceToInterfaceMap { | ||
strKey, ok := key.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to convert map key from type interface{} to string") | ||
} | ||
|
||
convertedValue, err := recursiveJSONMarshallable(value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
target[strKey] = convertedValue | ||
} | ||
return target, nil | ||
} | ||
|
||
if stringToInterfaceMap, ok := source.(map[string]interface{}); ok { | ||
target := map[string]interface{}{} | ||
for key, value := range stringToInterfaceMap { | ||
convertedValue, err := recursiveJSONMarshallable(value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
target[key] = convertedValue | ||
} | ||
return target, nil | ||
} | ||
|
||
return source, nil | ||
} |
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,108 @@ | ||
package models | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func Test_castRecursiveToMapStringInterface(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
source interface{} | ||
want interface{} | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "1 level", | ||
source: map[interface{}]interface{}{"aa": "bb"}, | ||
want: map[string]interface{}{"aa": "bb"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "1 level map[string]interface at top", | ||
source: map[string]interface{}{"aa": "bb"}, | ||
want: map[string]interface{}{"aa": "bb"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "2 levels", | ||
source: map[interface{}]interface{}{"aa": map[interface{}]interface{}{"aa": "bb"}, "b": "c"}, | ||
want: map[string]interface{}{"aa": map[string]interface{}{"aa": "bb"}, "b": "c"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "2 levels map[string]interface at top", | ||
source: map[string]interface{}{"aa": map[interface{}]interface{}{"aa": "bb"}, "b": "c"}, | ||
want: map[string]interface{}{"aa": map[string]interface{}{"aa": "bb"}, "b": "c"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Decoded from yml", | ||
source: map[interface{}]interface{}{ | ||
"bitrise.io.addons.optional.2": []interface{}{ | ||
map[interface{}]interface{}{ | ||
"addon_id": "addons-testing", | ||
}, | ||
}, | ||
"bitrise.io.addons.required": []interface{}{ | ||
map[interface{}]interface{}{ | ||
"addon_id": "addons-testing", | ||
"addon_options": map[interface{}]interface{}{ | ||
"required": true, | ||
"title": "Testing Addon", | ||
}, | ||
"addon_params": "--token TOKEN", | ||
}, | ||
map[interface{}]interface{}{ | ||
"addon_id": "addons-ship", | ||
"addon_options": map[interface{}]interface{}{ | ||
"required": true, | ||
"title": "Ship Addon", | ||
}, | ||
"addon_params": "--token TOKEN", | ||
}, | ||
}, | ||
}, | ||
want: map[string]interface{}{ | ||
"bitrise.io.addons.optional.2": []interface{}{ | ||
map[string]interface{}{ | ||
"addon_id": "addons-testing", | ||
}, | ||
}, | ||
"bitrise.io.addons.required": []interface{}{ | ||
map[string]interface{}{ | ||
"addon_id": "addons-testing", | ||
"addon_options": map[string]interface{}{ | ||
"required": true, | ||
"title": "Testing Addon", | ||
}, | ||
"addon_params": "--token TOKEN", | ||
}, | ||
map[string]interface{}{ | ||
"addon_id": "addons-ship", | ||
"addon_options": map[string]interface{}{ | ||
"required": true, | ||
"title": "Ship Addon", | ||
}, | ||
"addon_params": "--token TOKEN", | ||
}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := recursiveJSONMarshallable(tt.source) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("castRecursiveToMapStringInterface() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("castRecursiveToMapStringInterface() = %v, want %v, \n Diff %v", got, tt.want, cmp.Diff(got, tt.want)) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.