mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 14:45:49 +01:00
* fix(v3): warm up dialog types in go-json cache to prevent Windows panic Add FileFilter, OpenFileDialogOptions, SaveFileDialogOptions, and MessageDialogOptions to the init() warmup to prevent index out of bounds panic on Windows when these types are first unmarshaled. Fixes goccy/go-json#474 for Wails internal dialog types. * fix(v3): revert goccy/go-json to stdlib encoding/json to fix Windows panic goccy/go-json has a type address calculation bug on Windows that causes index out of bounds panic when decoding user-defined types for the first time. This reverts all runtime usages of goccy/go-json back to stdlib encoding/json. Test and benchmark files are left unchanged. Partially reverts PR #4843.
124 lines
1.9 KiB
Go
124 lines
1.9 KiB
Go
package application
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"encoding/json"
|
|
)
|
|
|
|
type Args struct {
|
|
rawData json.RawMessage
|
|
}
|
|
|
|
func (a *Args) UnmarshalJSON(data []byte) error {
|
|
a.rawData = data
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *Args) String() string { return string(a.rawData) }
|
|
|
|
func (a *Args) AsMap() *MapArgs {
|
|
m := make(map[string]interface{})
|
|
if a.rawData == nil {
|
|
return &MapArgs{
|
|
data: m,
|
|
}
|
|
}
|
|
err := json.Unmarshal(a.rawData, &m)
|
|
if err != nil {
|
|
return &MapArgs{
|
|
data: m,
|
|
}
|
|
}
|
|
return &MapArgs{data: m}
|
|
}
|
|
|
|
func (a *Args) ToStruct(str any) error {
|
|
return json.Unmarshal(a.rawData, str)
|
|
}
|
|
|
|
type MapArgs struct {
|
|
data map[string]interface{}
|
|
}
|
|
|
|
func (a *MapArgs) String(key string) *string {
|
|
if a == nil {
|
|
return nil
|
|
}
|
|
if val := a.data[key]; val != nil {
|
|
result := fmt.Sprintf("%v", val)
|
|
return &result
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *MapArgs) Int(s string) *int {
|
|
if a == nil {
|
|
return nil
|
|
}
|
|
if val := a.data[s]; val != nil {
|
|
return convertNumber[int](val)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func convertNumber[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](val any) *T {
|
|
if val == nil {
|
|
return nil
|
|
}
|
|
var result T
|
|
switch v := val.(type) {
|
|
case T:
|
|
result = v
|
|
case float64:
|
|
result = T(v)
|
|
default:
|
|
return nil
|
|
}
|
|
return &result
|
|
}
|
|
|
|
func (a *MapArgs) UInt8(s string) *uint8 {
|
|
if a == nil {
|
|
return nil
|
|
}
|
|
if val := a.data[s]; val != nil {
|
|
return convertNumber[uint8](val)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *MapArgs) UInt(s string) *uint {
|
|
if a == nil {
|
|
return nil
|
|
}
|
|
if val := a.data[s]; val != nil {
|
|
return convertNumber[uint](val)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *MapArgs) Float64(s string) *float64 {
|
|
if a == nil {
|
|
return nil
|
|
}
|
|
if val := a.data[s]; val != nil {
|
|
if result, ok := val.(float64); ok {
|
|
return &result
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *MapArgs) Bool(s string) *bool {
|
|
if a == nil {
|
|
return nil
|
|
}
|
|
if val := a.data[s]; val != nil {
|
|
if result, ok := val.(bool); ok {
|
|
return &result
|
|
}
|
|
}
|
|
return nil
|
|
}
|