mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 03:01:45 +08:00
If a field is exported, generate json even in the absence of tags (#3678)
* if no JSON tag, check if field is exported * add no-tags binding test case * update changelog for #3678
This commit is contained in:
parent
2b35861aee
commit
fe1f23b0fd
@ -350,7 +350,10 @@ func (b *Bindings) hasExportedJSONFields(typeOf reflect.Type) bool {
|
||||
for i := 0; i < typeOf.NumField(); i++ {
|
||||
jsonFieldName := ""
|
||||
f := typeOf.Field(i)
|
||||
jsonTag := f.Tag.Get("json")
|
||||
jsonTag, hasTag := f.Tag.Lookup("json")
|
||||
if !hasTag && f.IsExported() {
|
||||
return true
|
||||
}
|
||||
if len(jsonTag) == 0 {
|
||||
continue
|
||||
}
|
||||
|
59
v2/internal/binding/binding_test/binding_notags_test.go
Normal file
59
v2/internal/binding/binding_test/binding_notags_test.go
Normal file
@ -0,0 +1,59 @@
|
||||
package binding_test
|
||||
|
||||
type NoFieldTags struct {
|
||||
Name string
|
||||
Address string
|
||||
Zip *string
|
||||
Spouse *NoFieldTags
|
||||
}
|
||||
|
||||
func (n NoFieldTags) Get() NoFieldTags {
|
||||
return n
|
||||
}
|
||||
|
||||
var NoFieldTagsTest = BindingTest{
|
||||
name: "NoFieldTags",
|
||||
structs: []interface{}{
|
||||
&NoFieldTags{},
|
||||
},
|
||||
exemptions: nil,
|
||||
shouldError: false,
|
||||
want: `
|
||||
export namespace binding_test {
|
||||
export class NoFieldTags {
|
||||
Name: string;
|
||||
Address: string;
|
||||
Zip?: string;
|
||||
Spouse?: NoFieldTags;
|
||||
static createFrom(source: any = {}) {
|
||||
return new NoFieldTags(source);
|
||||
}
|
||||
constructor(source: any = {}) {
|
||||
if ('string' === typeof source) source = JSON.parse(source);
|
||||
this.Name = source["Name"];
|
||||
this.Address = source["Address"];
|
||||
this.Zip = source["Zip"];
|
||||
this.Spouse = this.convertValues(source["Spouse"], NoFieldTags);
|
||||
}
|
||||
|
||||
convertValues(a: any, classs: any, asMap: boolean = false): any {
|
||||
if (!a) {
|
||||
return a;
|
||||
}
|
||||
if (a.slice && a.map) {
|
||||
return (a as any[]).map(elem => this.convertValues(elem, classs));
|
||||
} else if ("object" === typeof a) {
|
||||
if (asMap) {
|
||||
for (const key of Object.keys(a)) {
|
||||
a[key] = new classs(a[key]);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
return new classs(a);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
}
|
@ -50,6 +50,7 @@ func TestBindings_GenerateModels(t *testing.T) {
|
||||
EntityWithDiffNamespacesTest,
|
||||
SpecialCharacterFieldTest,
|
||||
WithoutFieldsTest,
|
||||
NoFieldTagsTest,
|
||||
}
|
||||
|
||||
testLogger := &logger.Logger{}
|
||||
|
@ -553,7 +553,13 @@ func (t *TypeScriptify) getFieldOptions(structType reflect.Type, field reflect.S
|
||||
|
||||
func (t *TypeScriptify) getJSONFieldName(field reflect.StructField, isPtr bool) string {
|
||||
jsonFieldName := ""
|
||||
jsonTag := field.Tag.Get("json")
|
||||
jsonTag, hasTag := field.Tag.Lookup("json")
|
||||
if !hasTag && field.IsExported() {
|
||||
jsonFieldName = field.Name
|
||||
if isPtr {
|
||||
jsonFieldName += "?"
|
||||
}
|
||||
}
|
||||
if len(jsonTag) > 0 {
|
||||
jsonTagParts := strings.Split(jsonTag, ",")
|
||||
if len(jsonTagParts) > 0 {
|
||||
|
@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Fixed MacOS build to use `outputfilename` from wails.json. [#3200](https://github.com/wailsapp/wails/issues/3200)
|
||||
- Fixed file drop events on windows. Fixed in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi
|
||||
- Fixed doctor command not finding pkg-config on Solus. [PR #3670](https://github.com/wailsapp/wails/pull/3670) by [@ianmjones](https://github.com/ianmjones)
|
||||
- Fixed binding for struct fields that were exported but had no json tags. [PR #3678](https://github.com/wailsapp/wails/pull/3678)
|
||||
|
||||
## v2.9.1 - 2024-06-18
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user