-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Martin Guibert
committed
Apr 6, 2021
1 parent
7518020
commit 15e8699
Showing
57 changed files
with
482 additions
and
180 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package dctlcty | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/zclconf/go-cty/cty" | ||
ctyjson "github.com/zclconf/go-cty/cty/json" | ||
) | ||
|
||
func AsAttrs(val *cty.Value) CtyAttributes { | ||
if val == nil { | ||
return nil | ||
} | ||
bytes, _ := ctyjson.Marshal(*val, val.Type()) | ||
var attrs map[string]interface{} | ||
err := json.Unmarshal(bytes, &attrs) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return attrs | ||
} | ||
|
||
type CtyAttributes map[string]interface{} | ||
|
||
func (a *CtyAttributes) SafeDelete(path []string) { | ||
val := *a | ||
for i, key := range path { | ||
if i == len(path)-1 { | ||
delete(val, key) | ||
return | ||
} | ||
|
||
v, exists := val[key] | ||
if !exists { | ||
return | ||
} | ||
m, ok := v.(map[string]interface{}) | ||
if !ok { | ||
return | ||
} | ||
val = m | ||
} | ||
} | ||
|
||
func (a *CtyAttributes) SafeSet(path []string, value interface{}) error { | ||
val := *a | ||
for i, key := range path { | ||
if i == len(path)-1 { | ||
val[key] = value | ||
return nil | ||
} | ||
|
||
v, exists := val[key] | ||
if !exists { | ||
val[key] = map[string]interface{}{} | ||
v = val[key] | ||
} | ||
|
||
m, ok := v.(map[string]interface{}) | ||
if !ok { | ||
return errors.Errorf("Path %s cannot be set: %s is not a nested struct", strings.Join(path, "."), key) | ||
} | ||
val = m | ||
} | ||
return errors.New("Error setting value") // should not happen ? | ||
} |
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,214 @@ | ||
package dctlcty | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCtyAttributes_SafeDelete(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
attr CtyAttributes | ||
path []string | ||
expected CtyAttributes | ||
}{ | ||
{ | ||
name: "Delete existing", | ||
attr: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
path: []string{"test"}, | ||
expected: map[string]interface{}{ | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Delete existing nested", | ||
attr: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
path: []string{"nested", "testNested"}, | ||
expected: map[string]interface{}{ | ||
"test": "exists", | ||
|
||
"nested": map[string]interface{}{}, | ||
}, | ||
}, | ||
{ | ||
name: "Delete not existing", | ||
attr: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
path: []string{"test1"}, | ||
expected: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Delete not existing nested", | ||
attr: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
path: []string{"nested", "testNest"}, | ||
expected: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Delete not real nested", | ||
attr: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
path: []string{"test", "testNest"}, | ||
expected: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.attr.SafeDelete(tt.path) | ||
assert.Equal(t, tt.expected, tt.attr) | ||
}) | ||
} | ||
} | ||
|
||
func TestCtyAttributes_SafeSet(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
attr CtyAttributes | ||
path []string | ||
value interface{} | ||
expected CtyAttributes | ||
error error | ||
}{ | ||
{ | ||
name: "set existing", | ||
attr: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
path: []string{"test"}, | ||
value: "CHANGED", | ||
expected: map[string]interface{}{ | ||
"test": "CHANGED", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "set existing nested", | ||
attr: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
path: []string{"nested", "testNested"}, | ||
value: "CHANGED", | ||
expected: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "CHANGED", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Set not existing", | ||
attr: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
path: []string{"test1"}, | ||
value: "SET", | ||
expected: map[string]interface{}{ | ||
"test1": "SET", | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "SET not existing nested", | ||
attr: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
path: []string{"nested", "testNest"}, | ||
value: "SET", | ||
expected: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
"testNest": "SET", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Delete not real nested", | ||
attr: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
path: []string{"test", "testNest"}, | ||
value: "NOK", | ||
expected: map[string]interface{}{ | ||
"test": "exists", | ||
"nested": map[string]interface{}{ | ||
"testNested": "exists", | ||
}, | ||
}, | ||
error: errors.New("Path test.testNest cannot be set: test is not a nested struct"), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.attr.SafeSet(tt.path, tt.value) | ||
if tt.error != nil { | ||
assert.NotNil(t, err) | ||
assert.Equal(t, tt.error.Error(), err.Error()) | ||
} else { | ||
assert.Nil(t, err) | ||
} | ||
assert.Equal(t, tt.expected, tt.attr) | ||
}) | ||
} | ||
} |
This file was deleted.
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
Oops, something went wrong.