Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
refactor: pkg/property, pkg/layer, pkg/plugin (#101)
Browse files Browse the repository at this point in the history
* remove schema field from property.SchemaGroup and Item

* rename methods

* delete property.FieldUnsafeBuilder

* add methods to value

* refactor test

* refactor usage of references and Clone methods

* add cast method to property.Field

* add RemoveLayer method to layer/IDList

* add IDs and Properties methods to layer/List

* rename property/GroupList.GetGroup

* fix test

* add IDs method to property/List

* add nil check to property/SchemaField and plugin/Plugin

* add SchemaLoaderFromMap to property

* add PropertySchema method to manifest/Manifest
  • Loading branch information
rot1024 committed Jan 25, 2022
1 parent ddd0dbf commit 17a463d
Show file tree
Hide file tree
Showing 92 changed files with 2,297 additions and 1,257 deletions.
10 changes: 5 additions & 5 deletions internal/adapter/gql/gqlmodel/convert_property.go
Expand Up @@ -211,7 +211,7 @@ func ToPropertySchema(propertySchema *property.Schema) *PropertySchema {
pgroups := propertySchema.Groups()
groups := make([]*PropertySchemaGroup, 0, len(pgroups))
for _, g := range pgroups {
groups = append(groups, ToPropertySchemaGroup(g))
groups = append(groups, ToPropertySchemaGroup(g, propertySchema.ID()))
}

return &PropertySchema{
Expand Down Expand Up @@ -364,7 +364,7 @@ func ToMergedPropertyField(f *property.MergedField, s id.PropertySchemaID) *Merg
}
}

func ToPropertySchemaGroup(g *property.SchemaGroup) *PropertySchemaGroup {
func ToPropertySchemaGroup(g *property.SchemaGroup, s property.SchemaID) *PropertySchemaGroup {
if g == nil {
return nil
}
Expand All @@ -381,7 +381,7 @@ func ToPropertySchemaGroup(g *property.SchemaGroup) *PropertySchemaGroup {
}
return &PropertySchemaGroup{
SchemaGroupID: g.ID(),
SchemaID: g.Schema(),
SchemaID: s,
IsList: g.IsList(),
Title: g.Title().StringRef(),
Fields: fields,
Expand All @@ -405,7 +405,7 @@ func ToPropertyGroup(g *property.Group, p *property.Property, gl *property.Group

return &PropertyGroup{
ID: g.ID().ID(),
SchemaID: g.Schema(),
SchemaID: p.Schema(),
SchemaGroupID: g.SchemaGroup(),
Fields: fields,
}
Expand All @@ -424,7 +424,7 @@ func ToPropertyGroupList(g *property.GroupList, p *property.Property) *PropertyG

return &PropertyGroupList{
ID: g.ID().ID(),
SchemaID: g.Schema(),
SchemaID: p.Schema(),
SchemaGroupID: g.SchemaGroup(),
Groups: groups,
}
Expand Down
1 change: 1 addition & 0 deletions internal/infrastructure/fs/file.go
Expand Up @@ -180,6 +180,7 @@ func getAssetFileURL(base *url.URL, filename string) *url.URL {
return nil
}

// https://github.com/golang/go/issues/38351
b := *base
b.Path = path.Join(b.Path, filename)
return &b
Expand Down
2 changes: 2 additions & 0 deletions internal/infrastructure/gcs/file.go
Expand Up @@ -307,6 +307,8 @@ func getGCSObjectURL(base *url.URL, objectName string) *url.URL {
if base == nil {
return nil
}

// https://github.com/golang/go/issues/38351
b := *base
b.Path = path.Join(b.Path, objectName)
return &b
Expand Down
9 changes: 3 additions & 6 deletions internal/infrastructure/memory/plugin.go
Expand Up @@ -32,8 +32,7 @@ func (r *Plugin) FindByID(ctx context.Context, id id.PluginID, sids []id.SceneID
}
for _, p := range r.data {
if p.ID().Equal(id) && (p.ID().Scene() == nil || p.ID().Scene().Contains(sids)) {
p2 := *p
return &p2, nil
return p.Clone(), nil
}
}
return nil, rerror.ErrNotFound
Expand All @@ -51,8 +50,7 @@ func (r *Plugin) FindByIDs(ctx context.Context, ids []id.PluginID, sids []id.Sce
}
for _, p := range r.data {
if p.ID().Equal(id) && (p.ID().Scene() == nil || p.ID().Scene().Contains(sids)) {
p2 := *p
result = append(result, &p2)
result = append(result, p.Clone())
} else {
result = append(result, nil)
}
Expand All @@ -73,8 +71,7 @@ func (r *Plugin) Save(ctx context.Context, p *plugin.Plugin) error {
return nil
}
}
p2 := *p
r.data = append(r.data, &p2)
r.data = append(r.data, p.Clone())
return nil
}

Expand Down
Expand Up @@ -56,7 +56,7 @@ func AddSceneDefaultTile(ctx context.Context, c DBClient) error {
if g == nil || g.Count() > 0 {
continue
}
f := property.NewGroup().NewID().Schema(p.Schema(), id.PropertySchemaGroupID("tiles")).MustBuild()
f := property.NewGroup().NewID().SchemaGroup(id.PropertySchemaGroupID("tiles")).MustBuild()
g.Add(f, -1)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/mongodoc/dataset.go
Expand Up @@ -183,5 +183,5 @@ func toModelDatasetValue(v interface{}, t string) *dataset.Value {
if v2, ok := v.(bson.D); ok {
v = v2.Map()
}
return dataset.ValueTypeFrom(t).ValueFrom(v)
return dataset.ValueType(t).ValueFrom(v)
}
18 changes: 5 additions & 13 deletions internal/infrastructure/mongo/mongodoc/property.go
Expand Up @@ -35,7 +35,6 @@ type PropertyLinkDocument struct {
type PropertyItemDocument struct {
Type string
ID string
Schema string
SchemaGroup string
Groups []*PropertyItemDocument
Fields []*PropertyFieldDocument
Expand Down Expand Up @@ -149,7 +148,6 @@ func newPropertyItem(f property.Item) *PropertyItemDocument {
return &PropertyItemDocument{
Type: t,
ID: f.ID().String(),
Schema: f.Schema().String(),
SchemaGroup: string(f.SchemaGroup()),
Groups: items,
Fields: fields,
Expand Down Expand Up @@ -219,10 +217,9 @@ func toModelPropertyField(f *PropertyFieldDocument) *property.Field {
}

vt := property.ValueType(f.Type)
field := property.NewFieldUnsafe().
FieldUnsafe(id.PropertySchemaFieldID(f.Field)).
ValueUnsafe(property.NewOptionalValue(vt, toModelPropertyValue(f.Value, f.Type))).
LinksUnsafe(flinks).
field := property.NewField(property.FieldID(f.Field)).
Value(property.NewOptionalValue(vt, toModelPropertyValue(f.Value, f.Type))).
Links(flinks).
Build()

return field
Expand All @@ -236,16 +233,11 @@ func toModelPropertyItem(f *PropertyItemDocument) (property.Item, error) {
var i property.Item
var err error
var iid id.PropertyItemID
var sid id.PropertySchemaID

iid, err = id.PropertyItemIDFrom(f.ID)
if err != nil {
return nil, err
}
sid, err = id.PropertySchemaIDFrom(f.Schema)
if err != nil {
return nil, err
}
gid := id.PropertySchemaGroupID(f.SchemaGroup)

if f.Type == typePropertyItemGroup {
Expand All @@ -256,7 +248,7 @@ func toModelPropertyItem(f *PropertyItemDocument) (property.Item, error) {

i, err = property.NewGroup().
ID(iid).
Schema(sid, gid).
SchemaGroup(gid).
Fields(fields).
Build()
} else if f.Type == typePropertyItemGroupList {
Expand All @@ -273,7 +265,7 @@ func toModelPropertyItem(f *PropertyItemDocument) (property.Item, error) {

i, err = property.NewGroupList().
ID(iid).
Schema(sid, gid).
SchemaGroup(gid).
Groups(items).
Build()
}
Expand Down
21 changes: 10 additions & 11 deletions internal/infrastructure/mongo/mongodoc/property_schema.go
Expand Up @@ -14,6 +14,14 @@ type PropertySchemaDocument struct {
LinkableFields *PropertyLinkableFieldsDocument
}

type PropertySchemaGroupDocument struct {
ID string
Fields []*PropertySchemaFieldDocument
List bool
IsAvailableIf *PropertyConditonDocument
Title map[string]string
}

type PropertySchemaFieldDocument struct {
ID string
Type string
Expand Down Expand Up @@ -50,14 +58,6 @@ type PropertyConditonDocument struct {
Value interface{}
}

type PropertySchemaGroupDocument struct {
ID string
Fields []*PropertySchemaFieldDocument
List bool
IsAvailableIf *PropertyConditonDocument
Title map[string]string
}

type PropertySchemaConsumer struct {
Rows []*property.Schema
}
Expand Down Expand Up @@ -190,7 +190,7 @@ func (doc *PropertySchemaDocument) Model() (*property.Schema, error) {

groups := make([]*property.SchemaGroup, 0, len(doc.Groups))
for _, g := range doc.Groups {
g2, err := toModelPropertySchemaGroup(g, pid)
g2, err := g.Model()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func newPropertySchemaGroup(p *property.SchemaGroup) *PropertySchemaGroupDocumen
}
}

func toModelPropertySchemaGroup(d *PropertySchemaGroupDocument, sid id.PropertySchemaID) (*property.SchemaGroup, error) {
func (d *PropertySchemaGroupDocument) Model() (*property.SchemaGroup, error) {
if d == nil {
return nil, nil
}
Expand All @@ -264,7 +264,6 @@ func toModelPropertySchemaGroup(d *PropertySchemaGroupDocument, sid id.PropertyS

return property.NewSchemaGroup().
ID(id.PropertySchemaGroupID(d.ID)).
Schema(sid).
IsList(d.List).
Title(d.Title).
IsAvailableIf(toModelPropertyCondition(d.IsAvailableIf)).
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/interactor/scene.go
Expand Up @@ -111,7 +111,7 @@ func (i *Scene) Create(ctx context.Context, pid id.ProjectID, operator *usecase.
// add default tile
tiles := id.PropertySchemaGroupID("tiles")
g := p.GetOrCreateGroupList(schema, property.PointItemBySchema(tiles))
g.Add(property.NewGroup().NewID().Schema(schema.ID(), tiles).MustBuild(), -1)
g.Add(property.NewGroup().NewID().SchemaGroup(tiles).MustBuild(), -1)

scene, err := scene.New().
ID(sceneID).
Expand Down
6 changes: 2 additions & 4 deletions pkg/dataset/schema_builder.go
Expand Up @@ -74,12 +74,10 @@ func (b *SchemaBuilder) Fields(fields []*SchemaField) *SchemaBuilder {
}

if source := f.Source(); source == "" {
copied := *f
b.d.fields[f.ID()] = &copied
b.d.fields[f.ID()] = f.Clone()
b.d.order = append(b.d.order, f.ID())
} else if _, ok := sources[source]; !ok {
copied := *f
b.d.fields[f.ID()] = &copied
b.d.fields[f.ID()] = f.Clone()
b.d.order = append(b.d.order, f.ID())
sources[source] = struct{}{}
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/dataset/schema_field.go
Expand Up @@ -49,3 +49,16 @@ func (d *SchemaField) Source() (s string) {
}
return d.source
}

func (d *SchemaField) Clone() *SchemaField {
if d == nil {
return nil
}
return &SchemaField{
id: d.id,
name: d.name,
dataType: d.dataType,
source: d.source,
ref: d.ref.CopyRef(),
}
}
7 changes: 1 addition & 6 deletions pkg/dataset/schema_field_builder.go
Expand Up @@ -56,11 +56,6 @@ func (b *SchemaFieldBuilder) Source(source string) *SchemaFieldBuilder {
}

func (b *SchemaFieldBuilder) Ref(ref *SchemaID) *SchemaFieldBuilder {
if ref == nil {
b.d.ref = nil
} else {
ref2 := *ref
b.d.ref = &ref2
}
b.d.ref = ref.CopyRef()
return b
}
33 changes: 20 additions & 13 deletions pkg/dataset/value.go
Expand Up @@ -2,7 +2,6 @@ package dataset

import (
"net/url"
"strconv"

"github.com/reearth/reearth-backend/pkg/value"
)
Expand All @@ -13,6 +12,8 @@ type Coordinates = value.Coordinates
type Rect = value.Rect
type Polygon = value.Polygon

type ValueType value.Type

var (
ValueTypeUnknown = ValueType(value.TypeUnknown)
ValueTypeBool = ValueType(value.TypeBool)
Expand All @@ -27,10 +28,8 @@ var (
TypePolygon = ValueType(value.TypePolygon)
)

type ValueType value.Type

func ValueTypeFrom(t string) ValueType {
return ValueType(value.Type(t))
func (vt ValueType) Valid() bool {
return value.Type(vt).Default()
}

func (t ValueType) Default() bool {
Expand All @@ -52,10 +51,18 @@ func (vt ValueType) MustBeValue(i interface{}) *Value {
panic("invalid value")
}

func (vt ValueType) None() *OptionalValue {
return NewOptionalValue(vt, nil)
}

type Value struct {
v value.Value
}

func (v *Value) IsEmpty() bool {
return v == nil || v.v.IsEmpty()
}

func (v *Value) Clone() *Value {
if v == nil {
return nil
Expand All @@ -67,6 +74,10 @@ func (v *Value) Clone() *Value {
return &Value{v: *vv}
}

func (v *Value) Some() *OptionalValue {
return OptionalValueFrom(v)
}

func (v *Value) Type() ValueType {
if v == nil {
return ValueTypeUnknown
Expand Down Expand Up @@ -210,16 +221,12 @@ func (v *Value) ValuePolygon() *Polygon {
}

func ValueFromStringOrNumber(s string) *Value {
if vint, err := strconv.Atoi(s); err == nil {
return ValueTypeNumber.ValueFrom(vint)
if s == "true" || s == "false" || s == "TRUE" || s == "FALSE" || s == "True" || s == "False" {
return ValueTypeBool.ValueFrom(s)
}

if vfloat64, err := strconv.ParseFloat(s, 64); err == nil {
return ValueTypeNumber.ValueFrom(vfloat64)
}

if vbool, err := strconv.ParseBool(s); err == nil {
return ValueTypeBool.ValueFrom(vbool)
if v := ValueTypeNumber.ValueFrom(s); v != nil {
return v
}

return ValueTypeString.ValueFrom(s)
Expand Down

0 comments on commit 17a463d

Please sign in to comment.