diff --git a/go.mod b/go.mod index 6d8fa52cbfe0a8dc7162fd9c97ffc6c02ff3b2e0..deef35172dc9e98d7e77b17dc8e31bf33e006fc3 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( require ( cloud.google.com/go/compute v1.23.3 // indirect cloud.google.com/go/compute/metadata v0.2.3 // indirect + github.com/brianvoe/gofakeit/v6 v6.26.3 github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect @@ -67,4 +68,5 @@ require ( golang.org/x/text v0.14.0 // indirect google.golang.org/appengine v1.6.8 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) diff --git a/pkg/extension/action.go b/pkg/extension/action.go index be99916a35aa54e3f68d3f78b584e77cd02b4158..a8062d3ddda18f6a7990eb122262aac336d3a919 100644 --- a/pkg/extension/action.go +++ b/pkg/extension/action.go @@ -93,6 +93,8 @@ type Action struct { Extension string `mapstructure:"extension,omitempty" json:"extension,omitempty"` // Расширение Action string `mapstructure:"action,omitempty" json:"action"` // Рдентификатор действия Target ActionTarget `mapstructure:"target,omitempty" json:"target"` // Отображение результата действия + View ActionView `mapstructure:"view,omitempty" json:"view,omitempty"` // Отображение действия РІ интерфейсе + Order int32 `mapstructure:"order,omitempty" json:"order,omitempty"` // РџРѕСЂСЏРґРѕРє отображения действия РІ меню Parent string `mapstructure:"parent,omitempty" json:"parent,omitempty"` // Рдентификатор родительского действия (для отображения РІ меню) Name string `mapstructure:"name,omitempty" json:"name,omitempty"` // Название действия для отображения РІ интерфейсе (РїСѓРЅРєС‚ меню, РєРЅРѕРїРєР°). Description string `mapstructure:"description,omitempty" json:"description,omitempty"` // Описание действия для отображения РІ интерфейсе @@ -111,10 +113,18 @@ type Action struct { } func ActionToMap(action *Action) map[string]interface{} { + if action == nil { + return nil + } + res := make(map[string]interface{}) - _ = mapstructure.Decode(action, &res) + if err := mapstructure.Decode(action, &res); err != nil { + return nil + } + res["kind"] = int64(action.Kind.Number()) res["target"] = int64(action.Target.Number()) + res["view"] = int64(action.View.Number()) return res } @@ -217,6 +227,8 @@ func ActionFromPB(a *pb.Action) *Action { Extension: a.Extension, Action: a.Action, Target: a.Target, + View: a.View, + Order: a.Order, Parent: a.Parent, Name: a.Name, Description: a.Description, @@ -258,5 +270,7 @@ func ActionToPB(a *Action) *pb.Action { NavigationRoute: a.NavigationRoute, Autorun: a.Autorun, Confirm: a.Confirm, + View: a.View, + Order: a.Order, } } diff --git a/pkg/extension/action_test.go b/pkg/extension/action_test.go new file mode 100644 index 0000000000000000000000000000000000000000..d714a3993ad923cb56e78a0be43589f0d035e619 --- /dev/null +++ b/pkg/extension/action_test.go @@ -0,0 +1,86 @@ +package extension + +import ( + "testing" + + pb "git.perx.ru/perxis/perxis-go/proto/extensions" + "github.com/brianvoe/gofakeit/v6" + "github.com/stretchr/testify/require" +) + +func TestActionToProto(t *testing.T) { + t.Run("Filled struct", func(t *testing.T) { + var pbAction *pb.Action + err := gofakeit.Struct(&pbAction) + require.NoError(t, err) + + action := ActionFromPB(pbAction) + result := ActionToPB(action) + require.Equal(t, pbAction, result) + }) + + t.Run("Action is nil", func(t *testing.T) { + var pbAction *pb.Action + action := ActionFromPB(pbAction) + result := ActionToPB(action) + require.Equal(t, pbAction, result) + }) +} + +func TestActionToMap(t *testing.T) { + t.Run("Filled struct", func(t *testing.T) { + var action *Action + err := gofakeit.Struct(&action) + require.NoError(t, err) + dict := ActionToMap(action) + result, err := ActionFromMap(dict) + require.NoError(t, err) + require.Equal(t, action, result) + }) + + t.Run("Action is nil", func(t *testing.T) { + var action *Action + dict := ActionToMap(action) + result, err := ActionFromMap(dict) + require.NoError(t, err) + require.Equal(t, &Action{}, result) + }) +} + +func TestActionRequestToProto(t *testing.T) { + t.Run("Filled struct", func(t *testing.T) { + var pbAction *pb.ActionRequest + err := gofakeit.Struct(&pbAction) + require.NoError(t, err) + + action := ActionRequestFromPB(pbAction) + result := ActionRequestToPB(action) + require.Equal(t, pbAction, result) + }) + + t.Run("Action is nil", func(t *testing.T) { + var pbAction *pb.ActionRequest + action := ActionRequestFromPB(pbAction) + result := ActionRequestToPB(action) + require.Equal(t, pbAction, result) + }) +} + +func TestActionResponseToProto(t *testing.T) { + t.Run("Filled struct", func(t *testing.T) { + var pbAction *pb.ActionResponse + err := gofakeit.Struct(&pbAction) + require.NoError(t, err) + + action := ActionResponseFromPB(pbAction) + result := ActionResponseToPB(action) + require.Equal(t, pbAction, result) + }) + + t.Run("Action is nil", func(t *testing.T) { + var pbAction *pb.ActionResponse + action := ActionResponseFromPB(pbAction) + result := ActionResponseToPB(action) + require.Equal(t, pbAction, result) + }) +}