Skip to content
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

WIP: Generic ArrayMap #2749

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions pkg/cnab/config-adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,11 @@ func (c *ManifestConverter) generateParameterSources(b *cnab.ExtendedBundle) cna
ps["porter-state"] = c.generateOutputParameterSource("porter-state")

// bundle.outputs.OUTPUT
if b.Parameters == nil {
b.Parameters = make(map[string]bundle.Parameter)
}
for _, outputDef := range c.Manifest.GetTemplatedOutputs() {
wiringName, p, def := c.generateOutputWiringParameter(*b, outputDef.Name)
if b.Parameters == nil {
b.Parameters = make(map[string]bundle.Parameter, 1)
}
b.Parameters[wiringName] = p
b.Definitions[wiringName] = &def

Expand All @@ -555,9 +555,6 @@ func (c *ManifestConverter) generateParameterSources(b *cnab.ExtendedBundle) cna
// bundle.dependencies.DEP.outputs.OUTPUT
for _, ref := range c.Manifest.GetTemplatedDependencyOutputs() {
wiringName, p, def := c.generateDependencyOutputWiringParameter(ref)
if b.Parameters == nil {
b.Parameters = make(map[string]bundle.Parameter, 1)
}
b.Parameters[wiringName] = p
b.Definitions[wiringName] = &def

Expand Down
20 changes: 8 additions & 12 deletions pkg/cnab/provider/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ func TestRuntime_loadCredentials(t *testing.T) {

r.TestConfig.TestContext.AddTestFile("testdata/db-creds.json", "/db-creds.json")

cs1 := storage.NewCredentialSet("", "mycreds", secrets.SourceMap{
Name: "password",
Source: secrets.Source{
Strategy: secrets.SourceSecret,
Hint: "password",
},
cs1 := storage.NewCredentialSet("", "mycreds")
cs1.SetStrategy("password", secrets.Source{
Strategy: secrets.SourceSecret,
Hint: "password",
})

err := r.credentials.InsertCredentialSet(context.Background(), cs1)
Expand Down Expand Up @@ -135,12 +133,10 @@ func TestRuntime_loadCredentials_WithApplyTo(t *testing.T) {

r.TestCredentials.AddSecret("password", "mypassword")

cs1 := storage.NewCredentialSet("", "mycreds", secrets.SourceMap{
Name: "password",
Source: secrets.Source{
Strategy: secrets.SourceSecret,
Hint: "password",
},
cs1 := storage.NewCredentialSet("", "mycreds")
cs1.SetStrategy("password", secrets.Source{
Strategy: secrets.SourceSecret,
Hint: "password",
})

err := r.credentials.InsertCredentialSet(context.Background(), cs1)
Expand Down
201 changes: 201 additions & 0 deletions pkg/encoding/array_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package encoding

import (
"encoding/json"
"fmt"
"sort"

"gopkg.in/yaml.v3"
)

// MapElement is the in-memory representation of the item when stored in a map.
type MapElement interface {
ToArrayEntry(key string) ArrayElement
}

// ArrayElement is the representation of the item when encoded to an array in yaml or json.
type ArrayElement interface {
GetKey() string
ToMapEntry() MapElement
}

// ArrayMap is a map that is represented as an array when marshaled.
type ArrayMap[T MapElement, K ArrayElement] struct {
items map[string]T
}

func MakeArrayMap[T MapElement, K ArrayElement](len int) ArrayMap[T, K] {
return ArrayMap[T, K]{
items: make(map[string]T, len),
}
}

func (m *ArrayMap[T, K]) Len() int {
if m == nil {
return 0
}
return len(m.items)
}

func (m *ArrayMap[T, K]) Items() map[string]T {
if m == nil {
return nil
}

result := make(map[string]T, len(m.items))
for k, v := range m.items {
result[k] = v
}
return result
}

func (m *ArrayMap[T, K]) ItemsSorted() []K {
if m == nil {
return nil
}

result := make([]K, len(m.items))
i := 0
for k, v := range m.items {
// I can't figure out how to constrain T such that ToArrayEntry returns K, so I'm doing a cast
result[i] = v.ToArrayEntry(k).(K)
i++
}
sort.SliceStable(result, func(i, j int) bool {
return result[i].GetKey() < result[j].GetKey()
})

return result
}

func (m *ArrayMap[T, K]) ItemsUnsafe() map[string]T {
if m == nil {
return nil
}

if m.items == nil {
m.items = make(map[string]T)
}

return m.items
}

func (m *ArrayMap[T, K]) Get(key string) (T, bool) {
if m == nil {
return *new(T), false
}

entry, ok := m.items[key]
return entry, ok
}

func (m *ArrayMap[T, K]) Set(key string, entry T) {
if m.items == nil {
m.items = make(map[string]T, 1)
}

m.items[key] = entry
}

func (m *ArrayMap[T, K]) Remove(key string) {
if m == nil {
return
}
delete(m.items, key)
}

func (m *ArrayMap[T, K]) MarshalRaw() interface{} {
if m == nil {
return nil
}

var raw []ArrayElement
if m.items == nil {
return raw
}

raw = make([]ArrayElement, 0, len(m.items))
for k, v := range m.items {
raw = append(raw, v.ToArrayEntry(k))
}
sort.SliceStable(raw, func(i, j int) bool {
return raw[i].GetKey() < raw[j].GetKey()
})
return raw
}

func (m *ArrayMap[T, K]) UnmarshalRaw(raw []K) error {
// If there's nothing to import, stop early and allow the map to keep its original value
// So if someone unmarshalled into a nil map, it stays nil.
// This more closely matches how the stdlib encoders work
if len(raw) == 0 {
return nil
}

if m == nil {
*m = ArrayMap[T, K]{}
}

m.items = make(map[string]T, len(raw))
for _, rawItem := range raw {
if _, hasKey := m.items[rawItem.GetKey()]; hasKey {
return fmt.Errorf("cannot unmarshal source map: duplicate key found '%s'", rawItem.GetKey())
}
item := rawItem.ToMapEntry()
typedItem, ok := item.(T)
if !ok {
return fmt.Errorf("invalid ArrayMap generic types, ArrayElement %T returned a %T from ToMapEntry(), when it should return %T", rawItem, item, *new(T))
}
m.items[rawItem.GetKey()] = typedItem
}
return nil
}

func (m *ArrayMap[T, K]) MarshalJSON() ([]byte, error) {
raw := m.MarshalRaw()
return json.Marshal(raw)
}

func (m *ArrayMap[T, K]) UnmarshalJSON(data []byte) error {
var raw []K
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}
return m.UnmarshalRaw(raw)
}

func (m *ArrayMap[T, K]) UnmarshalYAML(value *yaml.Node) error {
var raw []K
if err := value.Decode(&raw); err != nil {
return err
}
return m.UnmarshalRaw(raw)
}

func (m *ArrayMap[T, K]) MarshalYAML() (interface{}, error) {
if m == nil {
return nil, nil
}
return m.MarshalRaw(), nil
}

// Merge applies the specified values on top of a base set of values. When a
// name exists in both sets, use the value from the overrides
func (m *ArrayMap[T, K]) Merge(overrides *ArrayMap[T, K]) *ArrayMap[T, K] {
result := make(map[string]T, m.Len())
if m != nil {
for k, v := range m.items {
result[k] = v
}
}

if overrides != nil {
// If the name is in the base, overwrite its value with the override provided
for k, v := range overrides.items {
result[k] = v
}
}

return &ArrayMap[T, K]{items: result}
}
127 changes: 127 additions & 0 deletions pkg/encoding/array_map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package encoding

import (
"encoding/json"
"os"
"testing"

"get.porter.sh/porter/pkg/yaml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestArrayMap_Merge(t *testing.T) {
m := &ArrayMap[TestMapEntry, TestArrayEntry]{}
m.Set("first", TestMapEntry{Value: "base first"})
m.Set("second", TestMapEntry{Value: "base second"})
m.Set("third", TestMapEntry{Value: "base third"})

result := m.Merge(&ArrayMap[TestMapEntry, TestArrayEntry]{})
require.Equal(t, 3, result.Len())
_, ok := result.Get("fourth")
assert.False(t, ok, "fourth should not be present in the base set")

wantFourth := TestMapEntry{Value: "new fourth"}
fourthMap := &ArrayMap[TestMapEntry, TestArrayEntry]{items: map[string]TestMapEntry{"fourth": wantFourth}}
result = m.Merge(fourthMap)
require.Equal(t, 4, result.Len())
gotFourth, ok := result.Get("fourth")
require.True(t, ok, "fourth should be present in the merged set")
assert.Equal(t, wantFourth, gotFourth, "incorrect merged value for fourth")

wantSecond := TestMapEntry{Value: "override second"}
secondMap := &ArrayMap[TestMapEntry, TestArrayEntry]{items: map[string]TestMapEntry{"second": wantSecond}}
result = m.Merge(secondMap)
require.Equal(t, 3, result.Len())
gotSecond, ok := result.Get("second")
require.True(t, ok, "second should be present in the merged set")
assert.Equal(t, wantSecond, gotSecond, "incorrect merged value for second")
}

func TestArrayMap_Unmarshal(t *testing.T) {
// TODO: add testcase for json
data, err := os.ReadFile("testdata/array.yaml")
require.NoError(t, err, "ReadFile failed")

var m ArrayMap[TestMapEntry, TestArrayEntry]
err = yaml.Unmarshal(data, &m)
require.NoError(t, err, "Unmarshal failed")

require.Equal(t, 2, m.Len(), "unexpected number of items defined")

gotA, ok := m.Get("aname")
require.True(t, ok, "aname was not defined")
wantA := TestMapEntry{Value: "stuff"}
assert.Equal(t, wantA, gotA, "unexpected aname defined")

gotB, ok := m.Get("bname")
require.True(t, ok, "password was not defined")
wantB := TestMapEntry{Value: "things"}
assert.Equal(t, wantB, gotB, "unexpected bname defined")
}

func TestArrayMap_Marshal(t *testing.T) {
// TODO: add testcase for json

m := &ArrayMap[TestMapEntry, TestArrayEntry]{}
m.Set("bname", TestMapEntry{Value: "things"})
m.Set("aname", TestMapEntry{Value: "stuff"})

wantData, err := os.ReadFile("testdata/array.yaml")
require.NoError(t, err, "ReadFile failed")

gotData, err := yaml.Marshal(m)
require.NoError(t, err, "Marshal failed")
assert.Equal(t, string(wantData), string(gotData))
}

func TestArrayMap_Unmarshal_DuplicateKeys(t *testing.T) {
data, err := os.ReadFile("testdata/array-with-duplicates.yaml")
require.NoError(t, err, "ReadFile failed")

var l ArrayMap[TestMapEntry, TestArrayEntry]
err = yaml.Unmarshal(data, &l)
require.ErrorContains(t, err, "cannot unmarshal source map: duplicate key found 'aname'")
}

// check that when we round trip a null ArrayMap, it stays null and isn't initialized to an _empty_ ArrayMap
// This impacts how it is marshaled later to yaml or json, because we often have fields tagged with omitempty
// and so it must be null to not be written out.
func TestArrayMap_RoundTrip_Empty(t *testing.T) {
wantData, err := os.ReadFile("testdata/array-empty.json")
require.NoError(t, err, "ReadFile failed")

var s struct {
Items *ArrayMap[TestMapEntry, TestArrayEntry] `json:"items"`
}
s.Items = &ArrayMap[TestMapEntry, TestArrayEntry]{}

gotData, err := json.Marshal(s)
require.NoError(t, err, "Marshal failed")
require.Equal(t, string(wantData), string(gotData), "empty ArrayMap should not marshal as empty, but nil so that it works with omitempty")

err = json.Unmarshal(gotData, &s)
require.NoError(t, err, "Unmarshal failed")
require.Nil(t, s.Items, "null ArrayMap should unmarshal as nil")
}

type TestMapEntry struct {
Value string `json:"value" yaml:"value"`
}

func (t TestMapEntry) ToArrayEntry(key string) ArrayElement {
return TestArrayEntry{Name: key, Value: t.Value}
}

type TestArrayEntry struct {
Name string `json:"name" yaml:"name"`
Value string `json:"value" yaml:"value"`
}

func (t TestArrayEntry) ToMapEntry() MapElement {
return TestMapEntry{Value: t.Value}
}

func (t TestArrayEntry) GetKey() string {
return t.Name
}
1 change: 1 addition & 0 deletions pkg/encoding/testdata/array-empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"items":null}
6 changes: 6 additions & 0 deletions pkg/encoding/testdata/array-with-duplicates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- name: aname
value: stuff
- name: bname
value: things
- name: aname
value: duplicate stuff
Loading