Skip to content

Commit

Permalink
(wip) add convert layer
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH committed Aug 5, 2024
1 parent bf70092 commit abd3fcb
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 13 deletions.
1 change: 0 additions & 1 deletion api/message_bus/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,6 @@ components:
# name: ysk
# path: github.com/IceWhaleTech/ZimaOS/ysk


YSKCardContent:
type: object
# x-go-type: ysk.YSKCardContent
Expand Down
52 changes: 40 additions & 12 deletions pkg/ysk/adapter.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package ysk

import (
"encoding/json"

"github.com/IceWhaleTech/CasaOS-MessageBus/codegen"
)

type CartType string

const (
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
55 changes: 55 additions & 0 deletions pkg/ysk/adapter_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit abd3fcb

Please sign in to comment.