Skip to content

Commit

Permalink
x/schema change model unions to simple types
Browse files Browse the repository at this point in the history
  • Loading branch information
widmogrod committed Dec 10, 2023
1 parent 0e2e3f5 commit a46c872
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 91 deletions.
6 changes: 3 additions & 3 deletions x/schema/dynamo_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ func ToDynamoDB(x Schema) types.AttributeValue {
},
func(x *Bool) types.AttributeValue {
return &types.AttributeValueMemberBOOL{
Value: x.B,
Value: bool(*x),
}
},
func(x *Number) types.AttributeValue {
return &types.AttributeValueMemberN{
Value: fmt.Sprintf("%f", x.N),
Value: fmt.Sprintf("%f", *x),
}
},
func(x *String) types.AttributeValue {
return &types.AttributeValueMemberS{
Value: x.S,
Value: string(*x),
}
},
func(x *Binary) types.AttributeValue {
Expand Down
6 changes: 3 additions & 3 deletions x/schema/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,21 +491,21 @@ func schemaToGo(x Schema, c *goConfig, path []string) (any, error) {
return x, nil
}

return x.B, nil
return bool(*x), nil
},
func(x *Number) (any, error) {
if _, ok := c.activeBuilder.(*UnionMap); ok {
return x, nil
}

return x.N, nil
return float64(*x), nil
},
func(x *String) (any, error) {
if _, ok := c.activeBuilder.(*UnionMap); ok {
return x, nil
}

return x.S, nil
return string(*x), nil
},
func(x *Binary) (any, error) {
if _, ok := c.activeBuilder.(*UnionMap); ok {
Expand Down
6 changes: 3 additions & 3 deletions x/schema/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ func toJSON(schema Schema, res *bytes.Buffer) error {
return nil
},
func(x *Bool) error {
if x.B {
if *x {
res.WriteString("true")
} else {
res.WriteString("false")
}
return nil
},
func(x *Number) error {
_, err := fmt.Fprintf(res, "%f", x.N)
_, err := fmt.Fprintf(res, "%f", *x)
if err != nil {
return err
}
return nil
},
func(x *String) error {
_, err := fmt.Fprintf(res, "%q", x.S)
_, err := fmt.Fprintf(res, "%q", *x)
if err != nil {
return err
}
Expand Down
15 changes: 8 additions & 7 deletions x/schema/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@ func MkNone() *None {
}

func MkBool(b bool) *Bool {
return &Bool{B: b}
return (*Bool)(&b)
}

func MkInt(x int) *Number {
return &Number{N: float64(x)}
v := float64(x)
return (*Number)(&v)
}

func MkFloat(x float64) *Number {
return &Number{N: x}
return (*Number)(&x)
}

func MkBinary(b []byte) *Binary {
return &Binary{B: b}
}

func MkString(s string) *String {
return &String{S: s}
return (*String)(&s)
}

func MkList(items ...Schema) *List {
Expand Down Expand Up @@ -79,9 +80,9 @@ type (
//go:generate go run ../../cmd/mkunion/main.go -name=Schema -skip-extension=schema,shape
type (
None struct{}
Bool struct{ B bool }
Number struct{ N float64 }
String struct{ S string }
Bool bool
Number float64
String string
Binary struct{ B []byte }
List struct {
Items []Schema
Expand Down
54 changes: 6 additions & 48 deletions x/schema/model_schema_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions x/schema/registry_well_defined_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func init() {
},
func(x Schema) time.Duration {
if v, ok := x.(*Number); ok {
return time.Duration(int64(v.N))
return time.Duration(int64(*v))
}

panic("invalid type")
Expand All @@ -21,7 +21,7 @@ func init() {
},
func(x Schema) time.Time {
if v, ok := x.(*String); ok {
t, _ := time.Parse(time.RFC3339Nano, v.S)
t, _ := time.Parse(time.RFC3339Nano, string(*v))
return t
}

Expand Down
50 changes: 25 additions & 25 deletions x/schema/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,84 +30,84 @@ func As[A int | int8 | int16 | int32 | int64 |
func(x *Bool) (A, bool) {
switch any(def).(type) {
case bool:
return any(x.B).(A), true
return any(bool(*x)).(A), true
}

return def, false
},
func(x *Number) (A, bool) {
switch any(def).(type) {
case float32:
return any(float32(x.N)).(A), true
return any(float32(*x)).(A), true
case float64:
return any(float64(x.N)).(A), true
return any(float64(*x)).(A), true
case int:
return any(int(x.N)).(A), true
return any(int(*x)).(A), true
case int8:
return any(int8(x.N)).(A), true
return any(int8(*x)).(A), true
case int16:
return any(int16(x.N)).(A), true
return any(int16(*x)).(A), true
case int32:
return any(int32(x.N)).(A), true
return any(int32(*x)).(A), true
case int64:
return any(int64(x.N)).(A), true
return any(int64(*x)).(A), true
case uint:
return any(uint(x.N)).(A), true
return any(uint(*x)).(A), true
case uint8:
return any(uint8(x.N)).(A), true
return any(uint8(*x)).(A), true
case uint16:
return any(uint16(x.N)).(A), true
return any(uint16(*x)).(A), true
case uint32:
return any(uint32(x.N)).(A), true
return any(uint32(*x)).(A), true
case uint64:
return any(uint64(x.N)).(A), true
return any(uint64(*x)).(A), true
}
return def, false
},
func(x *String) (A, bool) {
switch any(def).(type) {
case string:
return any(x.S).(A), true
return any(string(*x)).(A), true
case []byte:
return any([]byte(x.S)).(A), true
return any([]byte((*x))).(A), true
case float64:
v, err := strconv.ParseFloat(x.S, 64)
v, err := strconv.ParseFloat(string(*x), 64)
if err != nil {
return def, false
}
return any(v).(A), true
case float32:
v, err := strconv.ParseFloat(x.S, 32)
v, err := strconv.ParseFloat(string(*x), 32)
if err != nil {
return def, false
}
return any(float32(v)).(A), true
case int:
v, err := strconv.Atoi(x.S)
v, err := strconv.Atoi(string(*x))
if err != nil {
return def, false
}
return any(v).(A), true
case int8:
v, err := strconv.ParseInt(x.S, 10, 8)
v, err := strconv.ParseInt(string(*x), 10, 8)
if err != nil {
return def, false
}
return any(int8(v)).(A), true
case int16:
v, err := strconv.ParseInt(x.S, 10, 16)
v, err := strconv.ParseInt(string(*x), 10, 16)
if err != nil {
return def, false
}
return any(int16(v)).(A), true
case int32:
v, err := strconv.ParseInt(x.S, 10, 32)
v, err := strconv.ParseInt(string(*x), 10, 32)
if err != nil {
return def, false
}
return any(int32(v)).(A), true
case int64:
v, err := strconv.ParseInt(x.S, 10, 64)
v, err := strconv.ParseInt(string(*x), 10, 64)
if err != nil {
return def, false
}
Expand Down Expand Up @@ -286,7 +286,7 @@ func Compare(a, b Schema) int {
if *x == *y {
return 0
}
if x.B {
if *x {
return 1
}
return -1
Expand All @@ -299,7 +299,7 @@ func Compare(a, b Schema) int {
case *None, *Bool:
return 1
case *Number:
return int(x.N - y.N)
return int(*x - *y)
}

return -1
Expand All @@ -309,7 +309,7 @@ func Compare(a, b Schema) int {
case *None, *Bool, *Number:
return 1
case *String:
return strings.Compare(x.S, y.S)
return strings.Compare(string(*x), string(*y))
}

return -1
Expand Down

0 comments on commit a46c872

Please sign in to comment.