Skip to content

Commit

Permalink
Fix rendering of array types
Browse files Browse the repository at this point in the history
  • Loading branch information
fbbdev committed May 9, 2024
1 parent 939909a commit d21411d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 45 deletions.
9 changes: 8 additions & 1 deletion v3/internal/assetserver/bundledassets/runtime.debug.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion v3/internal/assetserver/bundledassets/runtime.js

Large diffs are not rendered by default.

43 changes: 17 additions & 26 deletions v3/internal/parser/render/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func (m *module) NeedsCreate(typ types.Type) bool {
return m.NeedsCreate(t.Underlying())
}

case *types.Array, *types.Map, *types.Slice:
return true
case *types.Array, *types.Pointer:
return m.NeedsCreate(typ.(interface{ Elem() types.Type }).Elem())

case *types.Pointer:
return m.NeedsCreate(t.Elem())
case *types.Map, *types.Slice:
return true

case *types.Struct:
info := m.collector.Struct(t)
Expand Down Expand Up @@ -88,16 +88,19 @@ func (m *module) JSCreateWithParams(typ types.Type, params string) string {
case *types.Alias:
return m.JSCreateWithParams(types.Unalias(typ), params)

case *types.Array:
case *types.Array, *types.Pointer:
pp, ok := m.postponedCreates.At(typ).(*postponed)
if !ok {
m.JSCreateWithParams(t.Elem(), params)
if ok {
return fmt.Sprintf("$$createType%d%s", pp.index, params)
}

createElement := m.JSCreateWithParams(typ.(interface{ Elem() types.Type }).Elem(), params)
if createElement != "$Create.Any" {
pp = &postponed{m.postponedCreates.Len(), params}
m.postponedCreates.Set(typ, pp)
return fmt.Sprintf("$$createType%d%s", pp.index, params)
}

return fmt.Sprintf("$$createType%d%s", pp.index, params)

case *types.Map:
pp, ok := m.postponedCreates.At(typ).(*postponed)
if !ok {
Expand Down Expand Up @@ -136,20 +139,11 @@ func (m *module) JSCreateWithParams(typ types.Type, params string) string {

return fmt.Sprintf("$$createType%d%s", pp.index, params)

case *types.Pointer:
pp, ok := m.postponedCreates.At(typ).(*postponed)
if ok {
return fmt.Sprintf("$$createType%d%s", pp.index, params)
}

createElement := m.JSCreateWithParams(t.Elem(), params)
if createElement != "$Create.Any" {
pp = &postponed{m.postponedCreates.Len(), params}
m.postponedCreates.Set(typ, pp)
return fmt.Sprintf("$$createType%d%s", pp.index, params)
case *types.Slice:
if types.Identical(typ, typeByteSlice) {
return "$Create.ByteSlice"
}

case *types.Slice:
pp, ok := m.postponedCreates.At(typ).(*postponed)
if !ok {
m.JSCreateWithParams(t.Elem(), params)
Expand Down Expand Up @@ -202,8 +196,8 @@ func (m *module) PostponedCreates() []string {
}

switch t := key.(type) {
case *types.Array:
result[pp.index] = fmt.Sprintf("%s$Create.Array(%s)", pre, m.JSCreateWithParams(t.Elem(), pp.params))
case *types.Array, *types.Slice:
result[pp.index] = fmt.Sprintf("%s$Create.Array(%s)", pre, m.JSCreateWithParams(t.(interface{ Elem() types.Type }).Elem(), pp.params))

case *types.Map:
result[pp.index] = fmt.Sprintf(
Expand Down Expand Up @@ -247,9 +241,6 @@ func (m *module) PostponedCreates() []string {
case *types.Pointer:
result[pp.index] = fmt.Sprintf("%s$Create.Nullable(%s)", pre, m.JSCreateWithParams(t.Elem(), pp.params))

case *types.Slice:
result[pp.index] = fmt.Sprintf("%s$Create.Array(%s)", pre, m.JSCreateWithParams(t.Elem(), pp.params))

case *types.Struct:
info := m.collector.Struct(t)
info.Collect()
Expand Down
13 changes: 2 additions & 11 deletions v3/internal/parser/render/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ func (m *module) JSDefault(typ types.Type, quoted bool) (result string) {
return result
}

case *types.Array:
if types.Identical(t.Elem(), types.Universe.Lookup("byte").Type()) {
// encoding/json marshals byte arrays as base64 strings
case *types.Array, *types.Slice:
if types.Identical(typ, typeByteSlice) {
return `""`
} else {
return "[]"
Expand All @@ -40,14 +39,6 @@ func (m *module) JSDefault(typ types.Type, quoted bool) (result string) {
case *types.Pointer:
return "null"

case *types.Slice:
if types.Identical(t.Elem(), types.Universe.Lookup("byte").Type()) {
// encoding/json marshals byte slices as base64 strings
return `""`
} else {
return "[]"
}

case *types.Struct:
return m.renderStructDefault(t)
}
Expand Down
13 changes: 7 additions & 6 deletions v3/internal/parser/render/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type aliasOrNamed interface {
Obj() *types.TypeName
}

// typeByteSlice caches the type-checker type for a slice of bytes.
var typeByteSlice = types.NewSlice(types.Universe.Lookup("byte").Type())

// JSType renders a Go type to its TypeScript representation,
// using the receiver's import map to resolve dependencies.
//
Expand Down Expand Up @@ -44,24 +47,22 @@ func (m *module) renderType(typ types.Type, quoted bool) (result string, nullabl
return m.renderNamedType(typ.(aliasOrNamed), quoted)

case *types.Array, *types.Slice:
elem := typ.(interface{ Elem() types.Type }).Elem()

null := ""
if _, isSlice := typ.(*types.Slice); isSlice && m.UseInterfaces {
// In interface mode, record the fact that encoding/json marshals nil slices as null.
null = " | null"
}

if types.Identical(elem, types.Universe.Lookup("byte").Type()) {
if types.Identical(typ, typeByteSlice) {
// encoding/json marshals byte arrays/slices as base64 strings
return "string" + null, null != ""
}

elemr, ptr := m.renderType(elem, false)
elem, ptr := m.renderType(typ.(interface{ Elem() types.Type }).Elem(), false)
if ptr {
return fmt.Sprintf("(%s)[]%s", elemr, null), null != ""
return fmt.Sprintf("(%s)[]%s", elem, null), null != ""
} else {
return fmt.Sprintf("%s[]%s", elemr, null), null != ""
return fmt.Sprintf("%s[]%s", elem, null), null != ""
}

case *types.Basic:
Expand Down
10 changes: 10 additions & 0 deletions v3/internal/runtime/desktop/@wailsio/runtime/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ export function Any(source) {
return /** @type {T} */(source);
}

/**
* ByteSlice is a creation function that replaces
* null strings with empty strings.
* @param {any} source
* @returns {string}
*/
export function ByteSlice(source) {
return /** @type {any} */((source == null) ? "" : source);
}

/**
* Array takes a creation function for an arbitrary type
* and returns an in-place creation function for an array
Expand Down

0 comments on commit d21411d

Please sign in to comment.