Skip to content

Commit

Permalink
Primative support for json.RawMessage.
Browse files Browse the repository at this point in the history
  • Loading branch information
arran4 committed Jan 15, 2021
1 parent ad72616 commit 29f6f2b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
17 changes: 17 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Field struct {
CanBeNull bool `json:"canBeNull"`
IsOptional bool `json:"isOptional"`
IsDate bool `json:"isDate"`
IsRaw bool `json:"isRaw"`
}

func (f *Field) Type(opts *Options, noSuffix bool) (out string) {
Expand All @@ -35,6 +36,10 @@ func (f *Field) Type(opts *Options, noSuffix bool) (out string) {
out = "Date"
}

if f.IsRaw {
out = "any"
}

if !noSuffix && f.CanBeNull {
out += " | null"
}
Expand Down Expand Up @@ -113,6 +118,10 @@ func (f *Field) DefaultValue() string {
return "new Date()"
}

if f.IsRaw {
return "null"
}

if f.TsType == "object" && f.ValType != "" {
return "new " + f.ValType + "()"
}
Expand Down Expand Up @@ -153,6 +162,11 @@ func (f *Field) setProps(sf reflect.StructField, sft reflect.Type) (ignore bool)

f.IsDate = isDate(sft) || len(tsTag) > 0 && tsTag[0] == "date" || sft.Kind() == reflect.Int64 && strings.HasSuffix(f.Name, "TS")

f.IsRaw = isRaw(sft) || len(tsTag) > 0 && tsTag[0] == "any"
if f.IsRaw {
f.CanBeNull = false
}

if len(tsTag) > 1 {
switch tsTag[1] {
case "no-null":
Expand Down Expand Up @@ -194,3 +208,6 @@ func TypeSuffix(t string, es6, as bool) string {
func isDate(t reflect.Type) bool {
return t.Name() == "Time" && t.PkgPath() == "time"
}
func isRaw(t reflect.Type) bool {
return t.Name() == "RawMessage" && t.PkgPath() == "encoding/json"
}
3 changes: 3 additions & 0 deletions s2ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func (s *StructToTS) addTypeFields(out *Struct, t reflect.Type) {
}

case k == reflect.Slice, k == reflect.Array:
if isRaw(sft) || tf.IsRaw {
break
}
tf.CanBeNull = k == reflect.Slice
tf.TsType, tf.ValType = "array", stripType(sft.Elem())

Expand Down
20 changes: 11 additions & 9 deletions s2ts_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package struct2ts_test

import (
"encoding/json"
"os"
"time"

Expand All @@ -12,15 +13,16 @@ type OtherStruct struct {
}

type ComplexStruct struct {
S string `json:"s,omitempty"`
I int `json:"i,omitempty"`
F float64 `json:"f,omitempty"`
TS *int64 `json:"ts,omitempty" ts:"date,null"`
T time.Time `json:"t,omitempty"` // automatically handled
NullOther *OtherStruct `json:"o,omitempty"`
NoNullOther *OtherStruct `json:"nno,omitempty" ts:",no-null"`
Data Data `json:"d"`
DataPtr *Data `json:"dp"`
S string `json:"s,omitempty"`
I int `json:"i,omitempty"`
F float64 `json:"f,omitempty"`
TS *int64 `json:"ts,omitempty" ts:"date,null"`
T time.Time `json:"t,omitempty"` // automatically handled
NullOther *OtherStruct `json:"o,omitempty"`
NoNullOther *OtherStruct `json:"nno,omitempty" ts:",no-null"`
Data Data `json:"d"`
DataPtr *Data `json:"dp"`
RawPtr *json.RawMessage `json:"rm"`
}

type Data map[string]interface{}
Expand Down

0 comments on commit 29f6f2b

Please sign in to comment.