Skip to content

Commit

Permalink
fix(json): Panic when json is null (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevgenypats authored Jan 11, 2023
1 parent 898e7bb commit 9165281
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions json/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,37 @@ func (t *ReverseTransformer) ReverseTransformValues(table *schema.Table, values
type Transformer struct{}

func (*Transformer) TransformBool(v *schema.Bool) any {
if v.Status != schema.Present {
return nil
}
return v.Bool
}

func (*Transformer) TransformBytea(v *schema.Bytea) any {
if v.Status != schema.Present {
return nil
}
return v.String()
}

func (*Transformer) TransformFloat8(v *schema.Float8) any {
if v.Status != schema.Present {
return nil
}
return v.Float
}

func (*Transformer) TransformInt8(v *schema.Int8) any {
if v.Status != schema.Present {
return nil
}
return v.Int
}

func (*Transformer) TransformInt8Array(v *schema.Int8Array) any {
if v.Status != schema.Present {
return nil
}
res := make([]int64, len(v.Elements))
for i, e := range v.Elements {
res[i] = e.Int
Expand All @@ -42,6 +57,9 @@ func (*Transformer) TransformInt8Array(v *schema.Int8Array) any {
}

func (*Transformer) TransformJSON(v *schema.JSON) any {
if v.Status != schema.Present {
return nil
}
var res any
if err := json.Unmarshal(v.Bytes, &res); err != nil {
panic(err)
Expand All @@ -50,10 +68,16 @@ func (*Transformer) TransformJSON(v *schema.JSON) any {
}

func (*Transformer) TransformText(v *schema.Text) any {
if v.Status != schema.Present {
return nil
}
return v.Str
}

func (*Transformer) TransformTextArray(v *schema.TextArray) any {
if v.Status != schema.Present {
return nil
}
res := make([]string, len(v.Elements))
for i, e := range v.Elements {
res[i] = e.Str
Expand All @@ -62,14 +86,23 @@ func (*Transformer) TransformTextArray(v *schema.TextArray) any {
}

func (*Transformer) TransformTimestamptz(v *schema.Timestamptz) any {
if v.Status != schema.Present {
return nil
}
return v.String()
}

func (*Transformer) TransformUUID(v *schema.UUID) any {
if v.Status != schema.Present {
return nil
}
return v.String()
}

func (*Transformer) TransformUUIDArray(v *schema.UUIDArray) any {
if v.Status != schema.Present {
return nil
}
res := make([]string, len(v.Elements))
for i, e := range v.Elements {
res[i] = e.String()
Expand All @@ -78,10 +111,16 @@ func (*Transformer) TransformUUIDArray(v *schema.UUIDArray) any {
}

func (*Transformer) TransformCIDR(v *schema.CIDR) any {
if v.Status != schema.Present {
return nil
}
return v.String()
}

func (*Transformer) TransformCIDRArray(v *schema.CIDRArray) any {
if v.Status != schema.Present {
return nil
}
res := make([]string, len(v.Elements))
for i, e := range v.Elements {
res[i] = e.String()
Expand All @@ -90,10 +129,16 @@ func (*Transformer) TransformCIDRArray(v *schema.CIDRArray) any {
}

func (*Transformer) TransformInet(v *schema.Inet) any {
if v.Status != schema.Present {
return nil
}
return v.String()
}

func (*Transformer) TransformInetArray(v *schema.InetArray) any {
if v.Status != schema.Present {
return nil
}
res := make([]string, len(v.Elements))
for i, e := range v.Elements {
res[i] = e.String()
Expand All @@ -102,10 +147,16 @@ func (*Transformer) TransformInetArray(v *schema.InetArray) any {
}

func (*Transformer) TransformMacaddr(v *schema.Macaddr) any {
if v.Status != schema.Present {
return nil
}
return v.String()
}

func (*Transformer) TransformMacaddrArray(v *schema.MacaddrArray) any {
if v.Status != schema.Present {
return nil
}
res := make([]string, len(v.Elements))
for i, e := range v.Elements {
res[i] = e.String()
Expand Down

0 comments on commit 9165281

Please sign in to comment.