From abd3fcbe20b848952d36d7257700fef08be1750a Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Mon, 5 Aug 2024 08:11:24 +0000 Subject: [PATCH] (wip) add convert layer --- api/message_bus/openapi.yaml | 1 - pkg/ysk/adapter.go | 52 ++++++++++++++++++++++++++-------- pkg/ysk/adapter_test.go | 55 ++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 pkg/ysk/adapter_test.go diff --git a/api/message_bus/openapi.yaml b/api/message_bus/openapi.yaml index b0e806d..2dc8aac 100644 --- a/api/message_bus/openapi.yaml +++ b/api/message_bus/openapi.yaml @@ -752,7 +752,6 @@ components: # name: ysk # path: github.com/IceWhaleTech/ZimaOS/ysk - YSKCardContent: type: object # x-go-type: ysk.YSKCardContent diff --git a/pkg/ysk/adapter.go b/pkg/ysk/adapter.go index bd99b20..a6e83bd 100644 --- a/pkg/ysk/adapter.go +++ b/pkg/ysk/adapter.go @@ -1,5 +1,11 @@ package ysk +import ( + "encoding/json" + + "github.com/IceWhaleTech/CasaOS-MessageBus/codegen" +) + type CartType string const ( @@ -25,10 +31,10 @@ const ( ) type YSKCard struct { - ID string - CardType CartType - RenderType RenderType - Content YSKCardContent + Id string `json:"id"` + CardType CartType `json:"cardType"` + RenderType RenderType `json:"renderType"` + Content YSKCardContent `json:"content"` } func (yskCard YSKCard) WithProgress(label string, progress int) YSKCard { @@ -43,12 +49,12 @@ func (yskCard YSKCard) WithProgress(label string, progress int) YSKCard { } type YSKCardContent struct { - TitleIcon string - TitleText string - BodyProgress *YSKCardProgress - BodyIconWithText *YSKCardIconWithText - BodyList []YSKCardListItem - FooterActions []YSKCardFooterAction + TitleIcon string `json:"titleIcon"` + TitleText string `json:"titleText"` + BodyProgress *YSKCardProgress `json:"bodyProgress,omitempty"` + BodyIconWithText *YSKCardIconWithText `json:"bodyIconWithText,omitempty"` + BodyList []YSKCardListItem `json:"bodyList,omitempty"` + FooterActions []YSKCardFooterAction `json:"footerActions,omitempty"` } type YSKCardProgress struct { @@ -75,8 +81,30 @@ type YSKCardFooterAction struct { } type YSKCardMessageBusAction struct { - Key string - Payload string + Key string `json:"key"` + Payload string `json:"payload"` } type YSKCardIcon = string + +func ToCodegenYSKCard(card YSKCard) (codegen.YSKCard, error) { + jsonBody, err := json.Marshal(card) + if err != nil { + return codegen.YSKCard{}, err + } + var yskCard codegen.YSKCard + err = json.Unmarshal(jsonBody, &yskCard) + + return yskCard, err +} + +func FromCodegenYSKCard(card codegen.YSKCard) (YSKCard, error) { + jsonBody, err := json.Marshal(card) + if err != nil { + return YSKCard{}, err + } + var yskCard YSKCard + err = json.Unmarshal(jsonBody, &yskCard) + + return yskCard, err +} diff --git a/pkg/ysk/adapter_test.go b/pkg/ysk/adapter_test.go new file mode 100644 index 0000000..8bb0a64 --- /dev/null +++ b/pkg/ysk/adapter_test.go @@ -0,0 +1,55 @@ +package ysk_test + +import ( + "encoding/json" + "reflect" + "testing" + + "github.com/IceWhaleTech/CasaOS-MessageBus/codegen" + "github.com/IceWhaleTech/CasaOS-MessageBus/pkg/ysk" + "gotest.tools/assert" +) + +func compareJSON(json1, json2 string) (bool, error) { + var v1, v2 interface{} + + if err := json.Unmarshal([]byte(json1), &v1); err != nil { + return false, err + } + + if err := json.Unmarshal([]byte(json2), &v2); err != nil { + return false, err + } + + return reflect.DeepEqual(v1, v2), nil +} + +func TestJsonIsSame(t *testing.T) { + domainObject := ysk.YSKCard{ + Id: "test-card", + CardType: ysk.CardTypeTask, + RenderType: ysk.RenderTypeCardIconTextNotice, + Content: ysk.YSKCardContent{ + TitleIcon: "test-icon", + TitleText: "test-title", + }, + } + codegenObject := codegen.YSKCard{ + Id: "test-card", + CardType: codegen.YSKCardCardTypeTask, + RenderType: codegen.YSKCardRenderTypeIconTextNotice, + Content: codegen.YSKCardContent{ + TitleIcon: "test-icon", + TitleText: "test-title", + }, + } + + json1, err := json.Marshal(domainObject) + assert.NilError(t, err) + json2, err := json.Marshal(codegenObject) + assert.NilError(t, err) + + equal, err := compareJSON(string(json1), string(json2)) + assert.NilError(t, err) + assert.Equal(t, equal, true) +}