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

Commit

Permalink
fix: cast values, rename value.OptionalValue (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 authored Dec 21, 2021
1 parent 16f20d3 commit ba4b18f
Show file tree
Hide file tree
Showing 23 changed files with 960 additions and 135 deletions.
18 changes: 18 additions & 0 deletions pkg/dataset/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ func (t ValueType) ValueFrom(i interface{}) *Value {
return &Value{v: *vv}
}

func (vt ValueType) MustBeValue(i interface{}) *Value {
if v := vt.ValueFrom(i); v != nil {
return v
}
panic("invalid value")
}

type Value struct {
v value.Value
}
Expand Down Expand Up @@ -81,6 +88,17 @@ func (v *Value) Interface() interface{} {
return v.v.Interface()
}

func (v *Value) Cast(vt ValueType) *Value {
if v == nil {
return nil
}
nv := v.v.Cast(value.Type(vt), nil)
if nv == nil {
return nil
}
return &Value{v: *nv}
}

func (v *Value) ValueBool() *bool {
if v == nil {
return nil
Expand Down
17 changes: 14 additions & 3 deletions pkg/dataset/value_optional.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package dataset
import "github.com/reearth/reearth-backend/pkg/value"

type OptionalValue struct {
ov value.OptionalValue
ov value.Optional
}

func NewOptionalValue(t ValueType, v *Value) *OptionalValue {
var vv *value.Value
if v != nil {
vv = &v.v
}
ov := value.NewOptionalValue(value.Type(t), vv)
ov := value.NewOptional(value.Type(t), vv)
if ov == nil {
return nil
}
Expand All @@ -22,7 +22,7 @@ func OptionalValueFrom(v *Value) *OptionalValue {
if v == nil {
return nil
}
ov := value.OptionalValueFrom(&v.v)
ov := value.OptionalFrom(&v.v)
if ov == nil {
return nil
}
Expand Down Expand Up @@ -76,3 +76,14 @@ func (ov *OptionalValue) Clone() *OptionalValue {
ov: *nov,
}
}

func (ov *OptionalValue) Cast(t ValueType) *OptionalValue {
if ov == nil {
return nil
}
vv := ov.ov.Cast(value.Type(t), nil)
if vv == nil {
return nil
}
return &OptionalValue{ov: *vv}
}
69 changes: 59 additions & 10 deletions pkg/dataset/value_optional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func TestNewNilableValue(t *testing.T) {
t: ValueTypeString,
v: ValueTypeString.ValueFrom("foo"),
},
want: &OptionalValue{ov: *value.OptionalValueFrom(value.TypeString.ValueFrom("foo", nil))},
want: &OptionalValue{ov: *value.OptionalFrom(value.TypeString.ValueFrom("foo", nil))},
},
{
name: "nil value",
args: args{
t: ValueTypeString,
},
want: &OptionalValue{ov: *value.NewOptionalValue(value.TypeString, nil)},
want: &OptionalValue{ov: *value.NewOptional(value.TypeString, nil)},
},
{
name: "invalid value",
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestOptionalValueFrom(t *testing.T) {
args: args{
v: ValueTypeString.ValueFrom("foo"),
},
want: &OptionalValue{ov: *value.NewOptionalValue(value.TypeString, value.TypeString.ValueFrom("foo", nil))},
want: &OptionalValue{ov: *value.NewOptional(value.TypeString, value.TypeString.ValueFrom("foo", nil))},
},
{
name: "empty value",
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestOptionalValue_Type(t *testing.T) {
}{
{
name: "ok",
value: &OptionalValue{ov: *value.NewOptionalValue(value.TypeBool, nil)},
value: &OptionalValue{ov: *value.NewOptional(value.TypeBool, nil)},
want: ValueTypeBool,
},
{
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestOptionalValue_Value(t *testing.T) {
}{
{
name: "ok",
value: &OptionalValue{ov: *value.OptionalValueFrom(value.TypeString.ValueFrom("foobar", nil))},
value: &OptionalValue{ov: *value.OptionalFrom(value.TypeString.ValueFrom("foobar", nil))},
want: ValueTypeString.ValueFrom("foobar"),
},
{
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestOptionalValue_TypeAndValue(t *testing.T) {
}{
{
name: "ok",
value: &OptionalValue{ov: *value.OptionalValueFrom(value.TypeString.ValueFrom("foobar", nil))},
value: &OptionalValue{ov: *value.OptionalFrom(value.TypeString.ValueFrom("foobar", nil))},
wantt: ValueTypeString,
wantv: ValueTypeString.ValueFrom("foobar"),
},
Expand Down Expand Up @@ -219,17 +219,17 @@ func TestOptionalValue_SetValue(t *testing.T) {
}{
{
name: "set",
value: &OptionalValue{ov: *value.OptionalValueFrom(value.TypeString.ValueFrom("foo", nil))},
value: &OptionalValue{ov: *value.OptionalFrom(value.TypeString.ValueFrom("foo", nil))},
args: args{v: ValueTypeString.ValueFrom("foobar")},
},
{
name: "set to nil",
value: &OptionalValue{ov: *value.NewOptionalValue(value.TypeString, nil)},
value: &OptionalValue{ov: *value.NewOptional(value.TypeString, nil)},
args: args{v: ValueTypeString.ValueFrom("foobar")},
},
{
name: "invalid value",
value: &OptionalValue{ov: *value.NewOptionalValue(value.TypeString, nil)},
value: &OptionalValue{ov: *value.NewOptional(value.TypeString, nil)},
args: args{v: ValueTypeNumber.ValueFrom(1)},
invalid: true,
},
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestOptionalValue_Clone(t *testing.T) {
{
name: "ok",
target: &OptionalValue{
ov: *value.NewOptionalValue(value.TypeString, value.TypeString.ValueFrom("foo", nil)),
ov: *value.NewOptional(value.TypeString, value.TypeString.ValueFrom("foo", nil)),
},
},
{
Expand All @@ -301,3 +301,52 @@ func TestOptionalValue_Clone(t *testing.T) {
})
}
}

func TestOptionalValue_Cast(t *testing.T) {
type args struct {
t ValueType
}
tests := []struct {
name string
target *OptionalValue
args args
want *OptionalValue
}{
{
name: "diff type",
target: &OptionalValue{ov: *value.OptionalFrom(value.TypeNumber.ValueFrom(1.1, nil))},
args: args{t: ValueTypeString},
want: &OptionalValue{ov: *value.OptionalFrom(value.TypeString.ValueFrom("1.1", nil))},
},
{
name: "same type",
target: &OptionalValue{ov: *value.OptionalFrom(value.TypeNumber.ValueFrom(1.1, nil))},
args: args{t: ValueTypeNumber},
want: &OptionalValue{ov: *value.OptionalFrom(value.TypeNumber.ValueFrom(1.1, nil))},
},
{
name: "failed to cast",
target: &OptionalValue{ov: *value.OptionalFrom(value.TypeLatLng.ValueFrom(LatLng{Lat: 1, Lng: 2}, nil))},
args: args{t: ValueTypeString},
want: &OptionalValue{ov: *value.NewOptional(value.TypeString, nil)},
},
{
name: "empty",
target: &OptionalValue{},
args: args{t: ValueTypeString},
want: nil,
},
{
name: "nil",
target: nil,
args: args{t: ValueTypeString},
want: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, tt.target.Cast(tt.args.t))
})
}
}
11 changes: 2 additions & 9 deletions pkg/property/merged.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,6 @@ func Merge(o *Property, p *Property, linked *id.DatasetID) *Merged {
return nil
}

// copy id
var linked2 *id.DatasetID
if linked != nil {
linked3 := *linked
linked2 = &linked3
}

var schema id.PropertySchemaID
if p != nil {
schema = p.Schema()
Expand All @@ -131,8 +124,8 @@ func Merge(o *Property, p *Property, linked *id.DatasetID) *Merged {
Original: o.IDRef(),
Parent: p.IDRef(),
Schema: schema,
Groups: mergeItems(o.Items(), p.Items(), linked2),
LinkedDataset: linked2,
Groups: mergeItems(o.Items(), p.Items(), linked.CopyRef()),
LinkedDataset: linked.CopyRef(),
}
}

Expand Down
31 changes: 22 additions & 9 deletions pkg/property/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package property

import (
"net/url"
"strconv"

"github.com/reearth/reearth-backend/pkg/value"
)
Expand Down Expand Up @@ -49,6 +48,13 @@ func (vt ValueType) ValueFrom(i interface{}) *Value {
return &Value{v: *v}
}

func (vt ValueType) MustBeValue(i interface{}) *Value {
if v := vt.ValueFrom(i); v != nil {
return v
}
panic("invalid value")
}

type Value struct {
v value.Value
}
Expand Down Expand Up @@ -89,6 +95,17 @@ func (v *Value) Interface() interface{} {
return v.v.Interface()
}

func (v *Value) Cast(vt ValueType) *Value {
if v == nil {
return nil
}
nv := v.v.Cast(value.Type(vt), types)
if nv == nil {
return nil
}
return &Value{v: *nv}
}

func (v *Value) ValueBool() *bool {
if v == nil {
return nil
Expand Down Expand Up @@ -200,16 +217,12 @@ func (v *Value) ValuePolygon() *Polygon {
}

func ValueFromStringOrNumber(s string) *Value {
if vint, err := strconv.Atoi(s); err == nil {
return ValueTypeNumber.ValueFrom(vint)
}

if vfloat64, err := strconv.ParseFloat(s, 64); err == nil {
return ValueTypeNumber.ValueFrom(vfloat64)
if s == "true" || s == "false" || s == "TRUE" || s == "FALSE" || s == "True" || s == "False" {
return ValueTypeBool.ValueFrom(s)
}

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
4 changes: 2 additions & 2 deletions pkg/property/value_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func NewValueAndDatasetValue(ty ValueType, d *dataset.Value, p *Value) *ValueAnd
}

if d != nil && ValueType(d.Type()) != ty {
d = nil
d = d.Cast(dataset.ValueType(ty))
}

if p != nil && p.Type() != ty {
p = nil
p = p.Cast(ty)
}

return &ValueAndDatasetValue{
Expand Down
Loading

0 comments on commit ba4b18f

Please sign in to comment.