Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
fix(server): related data convertion
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Dec 22, 2023
1 parent 3ba2035 commit aaa9ed9
Show file tree
Hide file tree
Showing 5 changed files with 437 additions and 252 deletions.
121 changes: 73 additions & 48 deletions server/cmsintegration/cmsintegrationv3/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmsintegrationv3

import (
"github.com/eukarya-inc/reearth-plateauview/server/cmsintegration/cmsintegrationcommon"
"github.com/oklog/ulid/v2"
cms "github.com/reearth/reearth-cms-api/go"
"github.com/samber/lo"
)

const modelPrefix = "plateau-"
Expand Down Expand Up @@ -230,51 +232,51 @@ func (i *GenericItem) CMSItem() *cms.Item {
}

type RelatedItem struct {
ID string `json:"id,omitempty" cms:"id"`
City string `json:"city,omitempty" cms:"city,reference"`
Assets map[string][]string `json:"assets,omitempty" cms:"-"`
ConvertedAssets map[string][]string `json:"converted,omitempty" cms:"-"`
Merged string `json:"merged,omitempty" cms:"merged,asset"`
ID string `json:"id,omitempty" cms:"id"`
City string `json:"city,omitempty" cms:"city,reference"`
Items map[string]RelatedItemDatum `json:"items,omitempty" cms:"-"`
Merged string `json:"merged,omitempty" cms:"merged,asset"`
// metadata
ConvertStatus *cms.Tag `json:"conv_status,omitempty" cms:"conv_status,tag,metadata"`
MergeStatus *cms.Tag `json:"merge_status,omitempty" cms:"merge_status,tag,metadata"`
Public bool `json:"public,omitempty" cms:"public,bool,metadata"`
ConvertStatus map[string]*cms.Tag `json:"conv_status,omitempty" cms:"-"`
MergeStatus *cms.Tag `json:"merge_status,omitempty" cms:"merge_status,tag,metadata"`
}

type RelatedItemDatum struct {
ID string `json:"id,omitempty" cms:"id"`
Asset []string `json:"asset,omitempty" cms:"asset,asset"`
Converted []string `json:"converted,omitempty" cms:"converted,asset"`
Description string `json:"description,omitempty" cms:"description,textarea"`
}

func RelatedItemFrom(item *cms.Item) (i *RelatedItem) {
i = &RelatedItem{}
item.Unmarshal(i)

for _, t := range relatedDataTypes {
v := item.FieldByKey(t).GetValue()
cv := item.FieldByKey(t + "_conv").GetValue()

var assets []string
if s := v.String(); s != nil {
assets = []string{*s}
} else if s := v.Strings(); s != nil {
assets = s
}
if i.Items == nil {
i.Items = map[string]RelatedItemDatum{}
}
if i.ConvertStatus == nil {
i.ConvertStatus = map[string]*cms.Tag{}
}

var conv []string
if s := cv.String(); s != nil {
conv = []string{*s}
} else if s := cv.Strings(); s != nil {
conv = s
for _, t := range relatedDataTypes {
g := item.FieldByKey(t).GetValue().String()
if g == nil {
continue
}

if len(assets) > 0 {
if i.Assets == nil {
i.Assets = map[string][]string{}
if group := item.Group(*g); group != nil && len(group.Fields) > 0 {
i.Items[t] = RelatedItemDatum{
ID: group.ID,
Asset: group.FieldByKey("asset").GetValue().Strings(),
Converted: group.FieldByKey("conv").GetValue().Strings(),
Description: lo.FromPtr(group.FieldByKey("description").GetValue().String()),
}
i.Assets[t] = append(i.Assets[t], assets...)
}

if len(conv) > 0 {
if i.ConvertedAssets == nil {
i.ConvertedAssets = map[string][]string{}
}
i.ConvertedAssets[t] = append(i.ConvertedAssets[t], conv...)
tag := item.MetadataFieldByKey(t + "_status").GetValue().Tag()
if tag != nil {
i.ConvertStatus[t] = tag
}
}

Expand All @@ -286,29 +288,52 @@ func (i *RelatedItem) CMSItem() *cms.Item {
cms.Marshal(i, item)

for _, t := range relatedDataTypes {
if asset, ok := i.Assets[t]; ok {
if d, ok := i.Items[t]; ok {
if d.ID == "" {
d.ID = ulid.Make().String()
}

if len(d.Asset) > 0 {
item.Fields = append(item.Fields, &cms.Field{
Key: "asset",
Type: "asset",
Value: d.Asset,
Group: d.ID,
})
}

if len(d.Converted) > 0 {
item.Fields = append(item.Fields, &cms.Field{
Key: "conv",
Type: "asset",
Value: d.Converted,
Group: d.ID,
})
}

if d.Description != "" {
item.Fields = append(item.Fields, &cms.Field{
Key: "description",
Type: "textarea",
Value: d.Description,
Group: d.ID,
})
}

item.Fields = append(item.Fields, &cms.Field{
Key: t,
Type: "asset",
Value: asset,
Type: "group",
Value: d.ID,
})
}

if conv, ok := i.ConvertedAssets[t]; ok {
item.Fields = append(item.Fields, &cms.Field{
Key: t + "_conv",
Type: "asset",
Value: conv,
if tag, ok := i.ConvertStatus[t]; ok {
item.MetadataFields = append(item.MetadataFields, &cms.Field{
Key: t + "_status",
Type: "tag",
Value: tag.Name,
})
}

// if pub, ok := i.Public[t]; ok {
// item.MetadataFields = append(item.MetadataFields, &cms.Field{
// Key: t + "_public",
// Type: "bool",
// Value: pub,
// })
// }
}

return item
Expand Down
77 changes: 56 additions & 21 deletions server/cmsintegration/cmsintegrationv3/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,80 +127,115 @@ func TestRelatedItemFrom(t *testing.T) {
ID: "id",
Fields: []*cms.Field{
{
Key: "park",
Key: "asset",
Type: "asset",
Value: []string{"PARK"},
Group: "park",
},
{
Key: "park_conv",
Key: "conv",
Type: "asset",
Value: []string{"PARK_CONV"},
Group: "park",
},
{
Key: "landmark",
Key: "park",
Type: "group",
Value: "park",
},
{
Key: "asset",
Type: "asset",
Value: []string{"LANDMARK"},
Group: "landmark",
},
{
Key: "landmark",
Type: "group",
Value: "landmark",
},
},
MetadataFields: []*cms.Field{
{
Key: "conv_status",
Key: "park_status",
Type: "tag",
Value: map[string]any{"id": "xxx", "name": string(ConvertionStatusSuccess)},
},
{
Key: "public",
Type: "bool",
Value: true,
Key: "merge_status",
Type: "tag",
Value: map[string]any{"id": "xxx", "name": string(ConvertionStatusSuccess)},
},
},
}

expected := &RelatedItem{
ID: "id",
Assets: map[string][]string{
"park": {"PARK"},
"landmark": {"LANDMARK"},
Items: map[string]RelatedItemDatum{
"park": {
ID: "park",
Asset: []string{"PARK"},
Converted: []string{"PARK_CONV"},
},
"landmark": {
ID: "landmark",
Asset: []string{"LANDMARK"},
},
},
ConvertedAssets: map[string][]string{
"park": {"PARK_CONV"},
ConvertStatus: map[string]*cms.Tag{
"park": {
ID: "xxx",
Name: string(ConvertionStatusSuccess),
},
},
ConvertStatus: &cms.Tag{
MergeStatus: &cms.Tag{
ID: "xxx",
Name: string(ConvertionStatusSuccess),
},
Public: true,
}

expected2 := &cms.Item{
ID: "id",
Fields: []*cms.Field{
{
Key: "park",
Key: "asset",
Type: "asset",
Value: []string{"PARK"},
Group: "park",
},
{
Key: "park_conv",
Key: "conv",
Type: "asset",
Value: []string{"PARK_CONV"},
Group: "park",
},
{
Key: "landmark",
Key: "park",
Type: "group",
Value: "park",
},
{
Key: "asset",
Type: "asset",
Value: []string{"LANDMARK"},
Group: "landmark",
},
{
Key: "landmark",
Type: "group",
Value: "landmark",
},
},
MetadataFields: []*cms.Field{
{
Key: "conv_status",
Key: "merge_status",
Type: "tag",
Value: "xxx",
},
{
Key: "public",
Type: "bool",
Value: true,
Key: "park_status",
Type: "tag",
Value: string(ConvertionStatusSuccess),
},
},
}
Expand Down
Loading

0 comments on commit aaa9ed9

Please sign in to comment.