diff --git a/go.mod b/go.mod index 63de9afb6a6cab76b4b155e7f090d3dc470806b9..fcffea8fcde51f29c2f1f78523643a4b9cdd0329 100644 --- a/go.mod +++ b/go.mod @@ -32,6 +32,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/davecgh/go-spew v1.1.1 // indirect github.com/go-kit/log v0.2.1 // indirect github.com/go-logfmt/logfmt v0.6.0 // indirect diff --git a/go.sum b/go.sum index 3273d5c5392761e9f4e31b6736f355f3bd1eba6d..fcb538d7446ef905cc38573b2c88d2496b342ac9 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/avast/retry-go/v4 v4.5.1 h1:AxIx0HGi4VZ3I02jr78j5lZ3M6x1E0Ivxa6b0pUUh github.com/avast/retry-go/v4 v4.5.1/go.mod h1:/sipNsvNB3RRuT5iNcb6h73nw3IBmXJ/H3XrCQYSOpc= github.com/bep/gowebp v0.2.0 h1:ZVfK8i9PpZqKHEmthQSt3qCnnHycbLzBPEsVtk2ch2Q= github.com/bep/gowebp v0.2.0/go.mod h1:ZhFodwdiFp8ehGJpF4LdPl6unxZm9lLFjxD3z2h2AgI= +github.com/brianvoe/gofakeit/v6 v6.26.3 h1:3ljYrjPwsUNAUFdUIr2jVg5EhKdcke/ZLop7uVg1Er8= +github.com/brianvoe/gofakeit/v6 v6.26.3/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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) + }) +}