Skip to content

Commit d21411d

Browse files
committed
Fix rendering of array types
1 parent 939909a commit d21411d

File tree

6 files changed

+45
-45
lines changed

6 files changed

+45
-45
lines changed

v3/internal/assetserver/bundledassets/runtime.debug.js

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v3/internal/assetserver/bundledassets/runtime.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v3/internal/parser/render/create.go

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ func (m *module) NeedsCreate(typ types.Type) bool {
4040
return m.NeedsCreate(t.Underlying())
4141
}
4242

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

46-
case *types.Pointer:
47-
return m.NeedsCreate(t.Elem())
46+
case *types.Map, *types.Slice:
47+
return true
4848

4949
case *types.Struct:
5050
info := m.collector.Struct(t)
@@ -88,16 +88,19 @@ func (m *module) JSCreateWithParams(typ types.Type, params string) string {
8888
case *types.Alias:
8989
return m.JSCreateWithParams(types.Unalias(typ), params)
9090

91-
case *types.Array:
91+
case *types.Array, *types.Pointer:
9292
pp, ok := m.postponedCreates.At(typ).(*postponed)
93-
if !ok {
94-
m.JSCreateWithParams(t.Elem(), params)
93+
if ok {
94+
return fmt.Sprintf("$$createType%d%s", pp.index, params)
95+
}
96+
97+
createElement := m.JSCreateWithParams(typ.(interface{ Elem() types.Type }).Elem(), params)
98+
if createElement != "$Create.Any" {
9599
pp = &postponed{m.postponedCreates.Len(), params}
96100
m.postponedCreates.Set(typ, pp)
101+
return fmt.Sprintf("$$createType%d%s", pp.index, params)
97102
}
98103

99-
return fmt.Sprintf("$$createType%d%s", pp.index, params)
100-
101104
case *types.Map:
102105
pp, ok := m.postponedCreates.At(typ).(*postponed)
103106
if !ok {
@@ -136,20 +139,11 @@ func (m *module) JSCreateWithParams(typ types.Type, params string) string {
136139

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

139-
case *types.Pointer:
140-
pp, ok := m.postponedCreates.At(typ).(*postponed)
141-
if ok {
142-
return fmt.Sprintf("$$createType%d%s", pp.index, params)
143-
}
144-
145-
createElement := m.JSCreateWithParams(t.Elem(), params)
146-
if createElement != "$Create.Any" {
147-
pp = &postponed{m.postponedCreates.Len(), params}
148-
m.postponedCreates.Set(typ, pp)
149-
return fmt.Sprintf("$$createType%d%s", pp.index, params)
142+
case *types.Slice:
143+
if types.Identical(typ, typeByteSlice) {
144+
return "$Create.ByteSlice"
150145
}
151146

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

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

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

250-
case *types.Slice:
251-
result[pp.index] = fmt.Sprintf("%s$Create.Array(%s)", pre, m.JSCreateWithParams(t.Elem(), pp.params))
252-
253244
case *types.Struct:
254245
info := m.collector.Struct(t)
255246
info.Collect()

v3/internal/parser/render/default.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ func (m *module) JSDefault(typ types.Type, quoted bool) (result string) {
2323
return result
2424
}
2525

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

43-
case *types.Slice:
44-
if types.Identical(t.Elem(), types.Universe.Lookup("byte").Type()) {
45-
// encoding/json marshals byte slices as base64 strings
46-
return `""`
47-
} else {
48-
return "[]"
49-
}
50-
5142
case *types.Struct:
5243
return m.renderStructDefault(t)
5344
}

v3/internal/parser/render/type.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type aliasOrNamed interface {
1515
Obj() *types.TypeName
1616
}
1717

18+
// typeByteSlice caches the type-checker type for a slice of bytes.
19+
var typeByteSlice = types.NewSlice(types.Universe.Lookup("byte").Type())
20+
1821
// JSType renders a Go type to its TypeScript representation,
1922
// using the receiver's import map to resolve dependencies.
2023
//
@@ -44,24 +47,22 @@ func (m *module) renderType(typ types.Type, quoted bool) (result string, nullabl
4447
return m.renderNamedType(typ.(aliasOrNamed), quoted)
4548

4649
case *types.Array, *types.Slice:
47-
elem := typ.(interface{ Elem() types.Type }).Elem()
48-
4950
null := ""
5051
if _, isSlice := typ.(*types.Slice); isSlice && m.UseInterfaces {
5152
// In interface mode, record the fact that encoding/json marshals nil slices as null.
5253
null = " | null"
5354
}
5455

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

60-
elemr, ptr := m.renderType(elem, false)
61+
elem, ptr := m.renderType(typ.(interface{ Elem() types.Type }).Elem(), false)
6162
if ptr {
62-
return fmt.Sprintf("(%s)[]%s", elemr, null), null != ""
63+
return fmt.Sprintf("(%s)[]%s", elem, null), null != ""
6364
} else {
64-
return fmt.Sprintf("%s[]%s", elemr, null), null != ""
65+
return fmt.Sprintf("%s[]%s", elem, null), null != ""
6566
}
6667

6768
case *types.Basic:

v3/internal/runtime/desktop/@wailsio/runtime/src/create.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ export function Any(source) {
2020
return /** @type {T} */(source);
2121
}
2222

23+
/**
24+
* ByteSlice is a creation function that replaces
25+
* null strings with empty strings.
26+
* @param {any} source
27+
* @returns {string}
28+
*/
29+
export function ByteSlice(source) {
30+
return /** @type {any} */((source == null) ? "" : source);
31+
}
32+
2333
/**
2434
* Array takes a creation function for an arbitrary type
2535
* and returns an in-place creation function for an array

0 commit comments

Comments
 (0)