From 848e6be30b808dd9515c0f9bb0ed86be67287c83 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Wed, 16 Sep 2020 19:34:00 +0200 Subject: [PATCH] Fix actionlog type codegen --- .../{types.gen.go => type_set.gen.go} | 34 +++++- pkg/actionlog/type_set.gen_test.go | 105 ++++++++++++++++++ pkg/actionlog/types.yaml | 3 + 3 files changed, 141 insertions(+), 1 deletion(-) rename pkg/actionlog/{types.gen.go => type_set.gen.go} (52%) create mode 100644 pkg/actionlog/type_set.gen_test.go create mode 100644 pkg/actionlog/types.yaml diff --git a/pkg/actionlog/types.gen.go b/pkg/actionlog/type_set.gen.go similarity index 52% rename from pkg/actionlog/types.gen.go rename to pkg/actionlog/type_set.gen.go index 153a07bdc..726268ba0 100644 --- a/pkg/actionlog/types.gen.go +++ b/pkg/actionlog/type_set.gen.go @@ -1,6 +1,12 @@ package actionlog -// Hello! This file is auto-generated. +// This file is auto-generated. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +// Definitions file that controls how this file is generated: +// pkg/actionlog/types.yaml type ( @@ -39,3 +45,29 @@ func (set ActionSet) Filter(f func(*Action) (bool, error)) (out ActionSet, err e return } + +// FindByID finds items from slice by its ID property +// +// This function is auto-generated. +func (set ActionSet) FindByID(ID uint64) *Action { + for i := range set { + if set[i].ID == ID { + return set[i] + } + } + + return nil +} + +// IDs returns a slice of uint64s from all items in the set +// +// This function is auto-generated. +func (set ActionSet) IDs() (IDs []uint64) { + IDs = make([]uint64, len(set)) + + for i := range set { + IDs[i] = set[i].ID + } + + return +} diff --git a/pkg/actionlog/type_set.gen_test.go b/pkg/actionlog/type_set.gen_test.go new file mode 100644 index 000000000..8cc640556 --- /dev/null +++ b/pkg/actionlog/type_set.gen_test.go @@ -0,0 +1,105 @@ +package actionlog + +// This file is auto-generated. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +// Definitions file that controls how this file is generated: +// pkg/actionlog/types.yaml + +import ( + "fmt" + "github.com/stretchr/testify/require" + "testing" +) + +func TestActionSetWalk(t *testing.T) { + var ( + value = make(ActionSet, 3) + req = require.New(t) + ) + + // check walk with no errors + { + err := value.Walk(func(*Action) error { + return nil + }) + req.NoError(err) + } + + // check walk with error + req.Error(value.Walk(func(*Action) error { return fmt.Errorf("walk error") })) +} + +func TestActionSetFilter(t *testing.T) { + var ( + value = make(ActionSet, 3) + req = require.New(t) + ) + + // filter nothing + { + set, err := value.Filter(func(*Action) (bool, error) { + return true, nil + }) + req.NoError(err) + req.Equal(len(set), len(value)) + } + + // filter one item + { + found := false + set, err := value.Filter(func(*Action) (bool, error) { + if !found { + found = true + return found, nil + } + return false, nil + }) + req.NoError(err) + req.Len(set, 1) + } + + // filter error + { + _, err := value.Filter(func(*Action) (bool, error) { + return false, fmt.Errorf("filter error") + }) + req.Error(err) + } +} + +func TestActionSetIDs(t *testing.T) { + var ( + value = make(ActionSet, 3) + req = require.New(t) + ) + + // construct objects + value[0] = new(Action) + value[1] = new(Action) + value[2] = new(Action) + // set ids + value[0].ID = 1 + value[1].ID = 2 + value[2].ID = 3 + + // Find existing + { + val := value.FindByID(2) + req.Equal(uint64(2), val.ID) + } + + // Find non-existing + { + val := value.FindByID(4) + req.Nil(val) + } + + // List IDs from set + { + val := value.IDs() + req.Equal(len(val), len(value)) + } +} diff --git a/pkg/actionlog/types.yaml b/pkg/actionlog/types.yaml new file mode 100644 index 000000000..071b30d92 --- /dev/null +++ b/pkg/actionlog/types.yaml @@ -0,0 +1,3 @@ +package: actionlog +types: + Action: