From 8dfb954e07376bcbb7b3e3fc172d27af0a2dcb91 Mon Sep 17 00:00:00 2001 From: rot1024 Date: Tue, 26 Mar 2024 00:40:01 +0900 Subject: [PATCH] fix(server): sample dataset were not correctly returned from datacatalogv3 --- server/datacatalog/datacatalogv3/cms.go | 22 +++--- server/datacatalog/datacatalogv3/cms_model.go | 58 +++++--------- .../datacatalogv3/cms_model_test.go | 15 ++-- server/datacatalog/datacatalogv3/conv.go | 75 ++++++++++++++----- .../datacatalog/datacatalogv3/conv_citygml.go | 9 +-- .../datacatalog/datacatalogv3/conv_dataset.go | 46 ------------ .../datacatalogv3/conv_dataset_plateau.go | 7 +- .../conv_dataset_plateau_seed.go | 4 +- .../datacatalogv3/conv_dataset_test.go | 66 ---------------- server/datacatalog/datacatalogv3/model.go | 11 ++- server/datacatalog/datacatalogv3/utils.go | 20 +++++ server/datacatalog/plateauapi/models.go | 32 +++++++- server/datacatalog/plateauapi/models_test.go | 6 +- 13 files changed, 168 insertions(+), 203 deletions(-) create mode 100644 server/datacatalog/datacatalogv3/utils.go diff --git a/server/datacatalog/datacatalogv3/cms.go b/server/datacatalog/datacatalogv3/cms.go index 78eb307cc..784e32f83 100644 --- a/server/datacatalog/datacatalogv3/cms.go +++ b/server/datacatalog/datacatalogv3/cms.go @@ -51,7 +51,7 @@ func (c *CMS) GetAll(ctx context.Context, project string) (*AllData, error) { return c.GetGenericItems(ctx, project) }) - sampleItemsChan := lo.Async2(func() ([]*GenericItem, error) { + sampleItemsChan := lo.Async2(func() ([]*PlateauFeatureItem, error) { return c.GetSampleItems(ctx, project) }) @@ -104,7 +104,13 @@ func (c *CMS) GetAll(ctx context.Context, project string) (*AllData, error) { if res := <-featureItemsChan; res.C != nil { return nil, fmt.Errorf("failed to get feature items (%s): %w", res.A, res.C) } else { - all.Plateau[res.A] = append(all.Plateau[res.A], res.B...) + for _, d := range res.B { + if d.Sample { + all.Sample = append(all.Sample, d) + } else { + all.Plateau[res.A] = append(all.Plateau[res.A], d) + } + } } } @@ -147,7 +153,7 @@ func (c *CMS) GetPlateauItems(ctx context.Context, project, feature string) ([]* items, err := getItemsAndConv( c.cms, ctx, project, modelPrefix+feature, func(i cms.Item) *PlateauFeatureItem { - return PlateauFeatureItemFrom(&i) + return PlateauFeatureItemFrom(&i, feature) }, ) return items, err @@ -180,18 +186,14 @@ func (c *CMS) GetGenericItems(ctx context.Context, project string) ([]*GenericIt return items, err } -func (c *CMS) GetSampleItems(ctx context.Context, project string) ([]*GenericItem, error) { +func (c *CMS) GetSampleItems(ctx context.Context, project string) ([]*PlateauFeatureItem, error) { items, err := getItemsAndConv( c.cms, ctx, project, modelPrefix+sampleModel, - func(i cms.Item) *GenericItem { - return GenericItemFrom(&i) + func(i cms.Item) *PlateauFeatureItem { + return PlateauFeatureItemFrom(&i, "") }, ) - for _, item := range items { - item.Category = "サンプルデータ" - } - return items, err } diff --git a/server/datacatalog/datacatalogv3/cms_model.go b/server/datacatalog/datacatalogv3/cms_model.go index dd9a9be05..b02cfa9e5 100644 --- a/server/datacatalog/datacatalogv3/cms_model.go +++ b/server/datacatalog/datacatalogv3/cms_model.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "slices" - "strings" "github.com/eukarya-inc/reearth-plateauview/server/cmsintegration/cmsintegrationcommon" "github.com/eukarya-inc/reearth-plateauview/server/datacatalog/datacatalogcommon" @@ -147,15 +146,16 @@ func (i *CityItem) IsPublicOrBeta() bool { } type PlateauFeatureItem struct { - ID string `json:"id,omitempty" cms:"id"` - City string `json:"city,omitempty" cms:"city,reference"` - CityGML string `json:"citygml,omitempty" cms:"citygml,-"` - Data []string `json:"data,omitempty" cms:"data,-"` - Desc string `json:"desc,omitempty" cms:"desc,textarea"` - Items []PlateauFeatureItemDatum `json:"items,omitempty" cms:"items,group"` - Dic string `json:"dic,omitempty" cms:"dic,textarea"` - Group string `json:"group,omitempty" cms:"group,text"` - MaxLOD string `json:"maxlod,omitempty" cms:"maxlod,-"` + ID string `json:"id,omitempty" cms:"id"` + City string `json:"city,omitempty" cms:"city,reference"` + CityGML string `json:"citygml,omitempty" cms:"citygml,-"` + Data []string `json:"data,omitempty" cms:"data,-"` + Desc string `json:"desc,omitempty" cms:"desc,textarea"` + Items []PlateauFeatureItemDatum `json:"items,omitempty" cms:"items,group"` + Dic string `json:"dic,omitempty" cms:"dic,textarea"` + Group string `json:"group,omitempty" cms:"group,text"` + MaxLOD string `json:"maxlod,omitempty" cms:"maxlod,-"` + FeatureType string `json:"feature_type,omitempty" cms:"feature_type,select"` // metadata Sample bool `json:"sample,omitempty" cms:"sample,bool,metadata"` Status *cms.Tag `json:"status,omitempty" cms:"status,select,metadata"` @@ -241,7 +241,7 @@ type DicEntry struct { Order *int `json:"order"` } -func PlateauFeatureItemFrom(item *cms.Item) (i *PlateauFeatureItem) { +func PlateauFeatureItemFrom(item *cms.Item, code string) (i *PlateauFeatureItem) { i = &PlateauFeatureItem{} item.Unmarshal(i) @@ -251,6 +251,14 @@ func PlateauFeatureItemFrom(item *cms.Item) (i *PlateauFeatureItem) { for ind, d := range i.Items { i.Items[ind].Data = valueToAssetURLs(item.FieldByKeyAndGroup("data", d.ID).GetValue()) } + if i.FeatureType != "" { + // e.g. "建築物モデル(bldg)" -> Name="建築物モデル", FeatureType="bldg" + if _, ft := getLastBracketContent(i.FeatureType); ft != "" { + i.FeatureType = ft + } + } else { + i.FeatureType = code + } return } @@ -298,10 +306,6 @@ type GenericItem struct { Items []GenericItemDataset `json:"items,omitempty" cms:"items,group"` OpenDataURL string `json:"open_data_url,omitempty" cms:"open_data_url,url"` Category string `json:"category,omitempty" cms:"category,select"` - // sample - CityGML string `json:"citygml,omitempty" cms:"citygml,asset"` - FeatureType string `json:"feature_type,omitempty" cms:"-"` - MaxLODURL string `json:"maxlodUrl,omitempty" cms:"-"` // metadata Status *cms.Tag `json:"status,omitempty" cms:"status,select,metadata"` Public bool `json:"public,omitempty" cms:"public,bool,metadata"` @@ -317,16 +321,6 @@ func (c *GenericItem) Stage() stage { return stageAlpha } -func (c *GenericItem) IsPublic() bool { - stage := c.Stage() - return stage == stageGA -} - -func (c *GenericItem) IsPublicOrBeta() bool { - stage := c.Stage() - return stage == stageGA || stage == stageBeta -} - type GenericItemDataset struct { ID string `json:"id,omitempty" cms:"id"` Name string `json:"name,omitempty" cms:"item_name,text"` @@ -344,20 +338,6 @@ func GenericItemFrom(item *cms.Item) (i *GenericItem) { for ind, d := range i.Items { i.Items[ind].Data = valueToAssetURL(item.FieldByKeyAndGroup("data", d.ID).GetValue()) } - - i.MaxLODURL = valueToAssetURL(item.FieldByKey("maxlod").GetValue()) - - // e.g. "建築物モデル(bldg)" -> Name="建築物モデル", FeatureType="bldg" - ft := lo.FromPtr(item.FieldByKey("feature_type").GetValue().String()) - if strings.Contains(ft, "(") && strings.Contains(ft, ")") { - name, s, _ := strings.Cut(ft, "(") - s, _, _ = strings.Cut(s, ")") - i.FeatureType = s - if i.Name == "" { - i.Name = name - } - } - return } diff --git a/server/datacatalog/datacatalogv3/cms_model_test.go b/server/datacatalog/datacatalogv3/cms_model_test.go index 3f88042df..a08eb3d37 100644 --- a/server/datacatalog/datacatalogv3/cms_model_test.go +++ b/server/datacatalog/datacatalogv3/cms_model_test.go @@ -42,14 +42,19 @@ func TestPlateauFeatureItemFrom(t *testing.T) { Group: "item2", Value: []any{"url4"}, // string is ignored }, + { + Key: "feature_type", + Value: "交通(道路)モデル(tran)", + }, }, } expected := &PlateauFeatureItem{ - ID: "id", - Data: []string{"url1", "url2"}, - CityGML: "url_citygml", - MaxLOD: "url_maxlod", + ID: "id", + Data: []string{"url1", "url2"}, + CityGML: "url_citygml", + MaxLOD: "url_maxlod", + FeatureType: "tran", Items: []PlateauFeatureItemDatum{ { ID: "item1", @@ -61,7 +66,7 @@ func TestPlateauFeatureItemFrom(t *testing.T) { }, } - assert.Equal(t, expected, PlateauFeatureItemFrom(item)) + assert.Equal(t, expected, PlateauFeatureItemFrom(item, "code")) } func TestGenericItemFrom(t *testing.T) { diff --git a/server/datacatalog/datacatalogv3/conv.go b/server/datacatalog/datacatalogv3/conv.go index d43de26f9..01586c9d6 100644 --- a/server/datacatalog/datacatalogv3/conv.go +++ b/server/datacatalog/datacatalogv3/conv.go @@ -58,13 +58,39 @@ func (all *AllData) Into() (res *plateauapi.InMemoryRepoContext, warning []strin } // plateau + plateauDatasetTypes := res.DatasetTypes.CodeMap(plateauapi.DatasetTypeCategoryPlateau) + plateauFeatureTypes := all.FeatureTypes.PlateauMap() for _, dt := range res.DatasetTypes[plateauapi.DatasetTypeCategoryPlateau] { - ft := all.FeatureTypes.FindPlateauByCode(dt.GetCode()) - datasets, w := convertPlateau(all.Plateau[dt.GetCode()], res.PlateauSpecs, dt, ft, ic) + datasets, w := convertPlateau( + all.Plateau[dt.GetCode()], + dt.GetCode(), + res.PlateauSpecs, + plateauDatasetTypes, + plateauFeatureTypes, + ic, + ) warning = append(warning, w...) res.Datasets.Append(plateauapi.DatasetTypeCategoryPlateau, datasets) } + // sample + sample := res.DatasetTypes.FindByCode("sample", plateauapi.DatasetTypeCategoryGeneric).(*plateauapi.GenericDatasetType) + if sample != nil { + datasets, w := convertPlateauRaw( + all.Sample, + "", + res.PlateauSpecs, + plateauDatasetTypes, + plateauFeatureTypes, + ic, + ) + warning = append(warning, w...) + res.Datasets.Append( + plateauapi.DatasetTypeCategoryGeneric, + plateauapi.ToDatasets(plateauapi.PlateauDatasetsToGenericDatasets(datasets, sample.ID, sample.Code, "sample")), + ) + } + // related { datasets, w := convertRelated(all.Related, res.DatasetTypes[plateauapi.DatasetTypeCategoryRelated], ic) @@ -72,19 +98,13 @@ func (all *AllData) Into() (res *plateauapi.InMemoryRepoContext, warning []strin res.Datasets.Append(plateauapi.DatasetTypeCategoryRelated, datasets) } - // generic and sample + // generic { - src := append(all.Generic, all.Sample...) - datasets, w := convertGeneric(src, res.DatasetTypes[plateauapi.DatasetTypeCategoryGeneric], ic) + datasets, w := convertGeneric(all.Generic, res.DatasetTypes[plateauapi.DatasetTypeCategoryGeneric], ic) warning = append(warning, w...) res.Datasets.Append(plateauapi.DatasetTypeCategoryGeneric, datasets) } - // move plateau sample data to generic - sampleType, _ := res.DatasetTypes.FindByCode( - "sample", plateauapi.DatasetTypeCategoryGeneric).(*plateauapi.GenericDatasetType) - movePlateauSampleDataToGeneric(res.Datasets, sampleType) - // citygml { var w []string @@ -110,16 +130,33 @@ func getWards(items []*PlateauFeatureItem, ic *internalContext) (res []*plateaua return } -func convertPlateau(items []*PlateauFeatureItem, specs []plateauapi.PlateauSpec, dt plateauapi.DatasetType, ft *FeatureType, ic *internalContext) (res []plateauapi.Dataset, warning []string) { - pdt, ok := dt.(*plateauapi.PlateauDatasetType) - if !ok { - warning = append(warning, fmt.Sprintf("plateau %s: invalid dataset type: %s", dt.GetCode(), dt.GetName())) - return - } - - layerNames := ic.layerNamesForType[pdt.Code] +func convertPlateau(items []*PlateauFeatureItem, code string, specs []plateauapi.PlateauSpec, dts map[string]plateauapi.DatasetType, fts map[string]*FeatureType, ic *internalContext) ([]plateauapi.Dataset, []string) { + res, w := convertPlateauRaw(items, code, specs, dts, fts, ic) + return plateauapi.ToDatasets(res), w +} +func convertPlateauRaw(items []*PlateauFeatureItem, code string, specs []plateauapi.PlateauSpec, dts map[string]plateauapi.DatasetType, fts map[string]*FeatureType, ic *internalContext) (res []*plateauapi.PlateauDataset, warning []string) { for _, ds := range items { + code := code + if ds.FeatureType != "" { + code = ds.FeatureType + } + + dt := dts[code] + ft := fts[code] + + if dt == nil || ft == nil { + warning = append(warning, fmt.Sprintf("plateau %s: invalid feature type: %s", ds.ID, code)) + continue + } + + pdt, ok := dt.(*plateauapi.PlateauDatasetType) + if !ok { + warning = append(warning, fmt.Sprintf("plateau %s: invalid dataset type: %s", dt.GetCode(), dt.GetName())) + return + } + + layerNames := ic.layerNamesForType[pdt.Code] area := ic.AreaContext(ds.City) if area == nil { warning = append(warning, fmt.Sprintf("plateau %s %s: invalid city: %s", ds.ID, pdt.Code, ds.City)) @@ -142,7 +179,7 @@ func convertPlateau(items []*PlateauFeatureItem, specs []plateauapi.PlateauSpec, FeatureType: ft, Year: ic.regYear, } - ds, w := ds.toDatasets(opts) + ds, w := ds.toDatasetsRaw(opts) warning = append(warning, w...) if ds != nil { res = append(res, ds...) diff --git a/server/datacatalog/datacatalogv3/conv_citygml.go b/server/datacatalog/datacatalogv3/conv_citygml.go index b87c954a0..fd57d7cad 100644 --- a/server/datacatalog/datacatalogv3/conv_citygml.go +++ b/server/datacatalog/datacatalogv3/conv_citygml.go @@ -73,7 +73,7 @@ func toCityGMLs(all *AllData, regYear int) (map[plateauapi.ID]*plateauapi.CityGM } for _, d := range all.Sample { - if d.MaxLODURL == "" || d.CityGML == "" { + if d.MaxLOD == "" || d.CityGML == "" { continue } @@ -87,12 +87,7 @@ func toCityGMLs(all *AllData, regYear int) (map[plateauapi.ID]*plateauapi.CityGM continue } - stage, _ := citygml.Admin.(map[string]any)["stage"].(string) - if stage == string(stageGA) && !d.IsPublic() || stage == string(stageBeta) && !d.IsPublicOrBeta() { - continue - } - - addCityGML(d.CityGML, d.MaxLODURL, d.FeatureType, citygml) + addCityGML(d.CityGML, d.MaxLOD, d.FeatureType, citygml) } return res, nil diff --git a/server/datacatalog/datacatalogv3/conv_dataset.go b/server/datacatalog/datacatalogv3/conv_dataset.go index 1561558d5..8fb31b884 100644 --- a/server/datacatalog/datacatalogv3/conv_dataset.go +++ b/server/datacatalog/datacatalogv3/conv_dataset.go @@ -225,49 +225,3 @@ func assetRootPath(p string) string { fn := strings.TrimSuffix(path.Base(p), path.Ext(p)) return path.Join(path.Dir(p), fn) } - -func sampleAdmin(sample bool) any { - if sample { - return map[string]any{"sample": true} - } - return nil -} - -func isSampleAdmin(admin any) bool { - m, _ := admin.(map[string]any) - if m == nil { - return false - } - - _, ok := m["sample"] - return ok -} - -func movePlateauSampleDataToGeneric(d plateauapi.Datasets, t *plateauapi.GenericDatasetType) { - if t == nil { - return - } - - plateau := d[plateauapi.DatasetTypeCategoryPlateau] - generic := d[plateauapi.DatasetTypeCategoryGeneric] - - for i, p := range plateau { - pd, ok := p.(*plateauapi.PlateauDataset) - if !ok || !isSampleAdmin(p.GetAdmin()) { - continue - } - - newID := pd.ID + "_sample" - tid := t.ID - tcode := t.Code - gd := plateauapi.PlateauDatasetToGenericDataset(pd, tid, tcode, newID) - gd.Admin.(map[string]any)["plateauTypeId"] = pd.TypeID - gd.Admin.(map[string]any)["plateauTypeCode"] = pd.TypeCode - - generic = append(generic, gd) - plateau = append(plateau[:i], plateau[i+1:]...) - } - - d[plateauapi.DatasetTypeCategoryPlateau] = plateau - d[plateauapi.DatasetTypeCategoryGeneric] = generic -} diff --git a/server/datacatalog/datacatalogv3/conv_dataset_plateau.go b/server/datacatalog/datacatalogv3/conv_dataset_plateau.go index 19637dda9..325c2409b 100644 --- a/server/datacatalog/datacatalogv3/conv_dataset_plateau.go +++ b/server/datacatalog/datacatalogv3/conv_dataset_plateau.go @@ -54,7 +54,12 @@ type ToPlateauDatasetsOptions struct { Year int } -func (i *PlateauFeatureItem) toDatasets(opts ToPlateauDatasetsOptions) (res []plateauapi.Dataset, warning []string) { +func (i *PlateauFeatureItem) toDatasets(opts ToPlateauDatasetsOptions) ([]plateauapi.Dataset, []string) { + res, w := i.toDatasetsRaw(opts) + return plateauapi.ToDatasets(res), w +} + +func (i *PlateauFeatureItem) toDatasetsRaw(opts ToPlateauDatasetsOptions) (res []*plateauapi.PlateauDataset, warning []string) { if !opts.Area.IsValid() { warning = append(warning, fmt.Sprintf("plateau %s: invalid area", i.ID)) return diff --git a/server/datacatalog/datacatalogv3/conv_dataset_plateau_seed.go b/server/datacatalog/datacatalogv3/conv_dataset_plateau_seed.go index 4c02ce614..0ef778dd1 100644 --- a/server/datacatalog/datacatalogv3/conv_dataset_plateau_seed.go +++ b/server/datacatalog/datacatalogv3/conv_dataset_plateau_seed.go @@ -78,8 +78,6 @@ func plateauDatasetSeedsFrom(i *PlateauFeatureItem, opts ToPlateauDatasetsOption // merge seeds with same ID res = mergeDatasetSeeds(res) - adminExtra := sampleAdmin(i.Sample) - // common for i := range res { res[i].DatasetType = opts.DatasetType @@ -92,7 +90,7 @@ func plateauDatasetSeedsFrom(i *PlateauFeatureItem, opts ToPlateauDatasetsOption opts.Area.CityItem.ID, opts.Area.CityItem.PlateauStage(opts.DatasetType.Code), opts.CMSURL, - adminExtra, + nil, ) res[i].LayerNames = opts.LayerNames res[i].Year = year diff --git a/server/datacatalog/datacatalogv3/conv_dataset_test.go b/server/datacatalog/datacatalogv3/conv_dataset_test.go index f685b59fb..af8578890 100644 --- a/server/datacatalog/datacatalogv3/conv_dataset_test.go +++ b/server/datacatalog/datacatalogv3/conv_dataset_test.go @@ -31,69 +31,3 @@ func TestStandardItemName(t *testing.T) { assert.Equal(t, "name(area)", standardItemName("name(area)", "", "area")) assert.Equal(t, "name name2(area)", standardItemName("name(area)", "name2", "area")) } - -func TestMovePlateauSampleDataToGeneric(t *testing.T) { - d := plateauapi.Datasets{ - plateauapi.DatasetTypeCategoryPlateau: []plateauapi.Dataset{ - &plateauapi.PlateauDataset{ - ID: "d_1", - }, - &plateauapi.PlateauDataset{ - ID: "d_2", - Admin: map[string]any{"sample": true}, - TypeID: "dt_bldg", - TypeCode: "bldg", - Items: []*plateauapi.PlateauDatasetItem{ - { - ID: "11", - Format: plateauapi.DatasetFormatCesium3dtiles, - Name: "LOD1", - URL: "hoge", - Layers: []string{"layer"}, - ParentID: "d_2", - }, - }, - }, - &plateauapi.PlateauDataset{ - ID: "d_3", - }, - }, - plateauapi.DatasetTypeCategoryGeneric: []plateauapi.Dataset{}, - } - - ty := &plateauapi.GenericDatasetType{ - ID: "dt_sample", - Code: "sample", - Name: "サンプルデータ", - } - - expected := &plateauapi.GenericDataset{ - ID: "d_2_sample", - Admin: map[string]any{ - "sample": true, - "plateauTypeId": plateauapi.ID("dt_bldg"), - "plateauTypeCode": "bldg", - }, - TypeID: "dt_sample", - TypeCode: "sample", - Items: []*plateauapi.GenericDatasetItem{ - { - ID: "11", - Format: plateauapi.DatasetFormatCesium3dtiles, - Name: "LOD1", - URL: "hoge", - Layers: []string{"layer"}, - ParentID: "d_2_sample", - }, - }, - } - - movePlateauSampleDataToGeneric(d, ty) - - assert.Len(t, d[plateauapi.DatasetTypeCategoryPlateau], 2) - assert.Len(t, d[plateauapi.DatasetTypeCategoryGeneric], 1) - - assert.Equal(t, expected, d[plateauapi.DatasetTypeCategoryGeneric][0]) - assert.Equal(t, "d_1", d[plateauapi.DatasetTypeCategoryPlateau][0].GetID().String()) - assert.Equal(t, "d_3", d[plateauapi.DatasetTypeCategoryPlateau][1].GetID().String()) -} diff --git a/server/datacatalog/datacatalogv3/model.go b/server/datacatalog/datacatalogv3/model.go index dc07efd70..051bd58ab 100644 --- a/server/datacatalog/datacatalogv3/model.go +++ b/server/datacatalog/datacatalogv3/model.go @@ -10,7 +10,7 @@ type AllData struct { City []*CityItem Related []*RelatedItem Generic []*GenericItem - Sample []*GenericItem + Sample []*PlateauFeatureItem Plateau map[string][]*PlateauFeatureItem GeospatialjpDataItems []*GeospatialjpDataItem CMSInfo CMSInfo @@ -50,6 +50,15 @@ type FeatureTypes struct { Generic []FeatureType } +func (ft FeatureTypes) PlateauMap() map[string]*FeatureType { + res := make(map[string]*FeatureType) + for _, f := range ft.Plateau { + f := f + res[f.Code] = &f + } + return res +} + func (ft FeatureTypes) FindPlateauByCode(code string) *FeatureType { for _, f := range ft.Plateau { if f.Code == code { diff --git a/server/datacatalog/datacatalogv3/utils.go b/server/datacatalog/datacatalogv3/utils.go new file mode 100644 index 000000000..fbea312e5 --- /dev/null +++ b/server/datacatalog/datacatalogv3/utils.go @@ -0,0 +1,20 @@ +package datacatalogv3 + +import "strings" + +func getLastBracketContent(s string) (string, string) { + if strings.Contains(s, "(") && strings.Contains(s, ")") { + name, s := cutStringRight(s, "(") + s, _, _ = strings.Cut(s, ")") + return name, s + } + + return "", "" +} + +func cutStringRight(s string, sep string) (string, string) { + if i := strings.LastIndex(s, sep); i >= 0 { + return s[:i], s[i+len(sep):] + } + return s, "" +} diff --git a/server/datacatalog/plateauapi/models.go b/server/datacatalog/plateauapi/models.go index fad80f7a5..a015b8d75 100644 --- a/server/datacatalog/plateauapi/models.go +++ b/server/datacatalog/plateauapi/models.go @@ -144,6 +144,15 @@ func (d DatasetTypes) All() []DatasetType { }) } +func (d DatasetTypes) CodeMap(cat DatasetTypeCategory) map[string]DatasetType { + res := make(map[string]DatasetType) + for _, ds := range d[cat] { + ds := ds + res[ds.GetCode()] = ds + } + return res +} + func (d DatasetTypes) DatasetTypesByCategory(cat DatasetTypeCategory) []DatasetType { if cat == "" { return d.All() @@ -275,9 +284,26 @@ func getVagueID(n any) string { return "" } -func PlateauDatasetToGenericDataset(p *PlateauDataset, typeID ID, typeCode string, newID ID) *GenericDataset { - if newID == "" { - newID = p.ID +func ToDatasets[T Dataset](p []T) []Dataset { + res := make([]Dataset, 0, len(p)) + for _, ds := range p { + res = append(res, ds) + } + return res +} + +func PlateauDatasetsToGenericDatasets(p []*PlateauDataset, typeID ID, typeCode, idSuffix string) []*GenericDataset { + res := make([]*GenericDataset, 0, len(p)) + for _, ds := range p { + res = append(res, PlateauDatasetToGenericDataset(ds, typeID, typeCode, idSuffix)) + } + return res +} + +func PlateauDatasetToGenericDataset(p *PlateauDataset, typeID ID, typeCode string, idSuffix string) *GenericDataset { + newID := p.ID + if idSuffix != "" { + newID = ID(string(p.ID) + "_" + idSuffix) } items := make([]*GenericDatasetItem, 0, len(p.Items)) diff --git a/server/datacatalog/plateauapi/models_test.go b/server/datacatalog/plateauapi/models_test.go index 6d600fec3..02d0d60b1 100644 --- a/server/datacatalog/plateauapi/models_test.go +++ b/server/datacatalog/plateauapi/models_test.go @@ -398,7 +398,7 @@ func TestPlateauDatasetToGenericDataset(t *testing.T) { } expected := &GenericDataset{ - ID: "d_1_", + ID: "d_1_sample", Admin: map[string]any{"sample": true}, TypeID: "dt_sample", TypeCode: "sample", @@ -409,12 +409,12 @@ func TestPlateauDatasetToGenericDataset(t *testing.T) { Name: "LOD1", URL: "hoge", Layers: []string{"layer"}, - ParentID: "d_1_", + ParentID: "d_1_sample", }, }, } - actual := PlateauDatasetToGenericDataset(d, "dt_sample", "sample", "d_1_") + actual := PlateauDatasetToGenericDataset(d, "dt_sample", "sample", "sample") assert.Equal(t, expected, actual) }