-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/Tom5521/gtk4tools | ||
|
||
go 1.22.4 | ||
go 1.22 | ||
|
||
require github.com/diamondburned/gotk4/pkg v0.3.0 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package walk | ||
|
||
import "reflect" | ||
|
||
type Iteration func(reflect.Value, reflect.StructField) bool | ||
|
||
func Into(t any, action Iteration) { | ||
value := reflect.ValueOf(t) | ||
if v := value.Kind(); v == reflect.Pointer || v == reflect.Interface { | ||
value = value.Elem() | ||
} | ||
|
||
rtype := reflect.TypeOf(t) | ||
if rtype.Kind() == reflect.Pointer { | ||
rtype = rtype.Elem() | ||
} | ||
Struct(value, rtype, action) | ||
} | ||
|
||
func Struct( | ||
value reflect.Value, | ||
str reflect.Type, | ||
action Iteration, | ||
) { | ||
for i := range str.NumField() { | ||
v := value.Field(i) | ||
t := str.Field(i) | ||
|
||
if !t.IsExported() { | ||
continue | ||
} | ||
|
||
if t.Type.Kind() == reflect.Struct { | ||
Struct(v, t.Type, action) | ||
continue | ||
} | ||
if !action(v, t) { | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package walk_test | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/Tom5521/gtk4tools/internal/walk" | ||
) | ||
|
||
type A struct { | ||
F1 string | ||
F2 bool | ||
F3 int | ||
B struct { | ||
F1 string | ||
C struct { | ||
F1 string | ||
} | ||
} | ||
} | ||
|
||
func TestUnmarshall(_ *testing.T) { | ||
str := A{ | ||
F1: "Meow in A", | ||
F2: true, | ||
F3: 20, | ||
} | ||
str.B.F1 = "Meow in B" | ||
str.B.C.F1 = "Meow in C" | ||
|
||
walk.Into( | ||
&str, | ||
func(v reflect.Value, _ reflect.StructField) bool { | ||
if v.String() == "Meow in C" { | ||
v.Set(reflect.ValueOf(v.String() + " | Edited")) | ||
} | ||
fmt.Println(v) | ||
return true | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package gtools_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Tom5521/gtk4tools/pkg/gtools" | ||
"github.com/diamondburned/gotk4/pkg/gtk/v4" | ||
) | ||
|
||
func ExampleFetchObjects() { | ||
const xml = `<?xml version="1.0" encoding="UTF-8"?> | ||
<interface> | ||
<requires lib="gtk" version="4.0"/> | ||
<object class="GtkWindow" id="window"> | ||
<property name="title">Hello World</property> | ||
</object> | ||
</interface>` | ||
|
||
type ui struct { | ||
Window *gtk.Window `gtk:"window"` | ||
} | ||
|
||
w := new(ui) | ||
gtools.FetchObjects(w, xml) | ||
|
||
fmt.Println(w.Window.Title()) // Hello World | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package gtools | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/Tom5521/gtk4tools/internal/walk" | ||
"github.com/diamondburned/gotk4/pkg/gtk/v4" | ||
) | ||
|
||
// What this function does is to obtain the tags of the provided structure | ||
// and with the content of the tag call gtk.Builder.GetObject | ||
// and assign it to its corresponding field, | ||
// if the field has no tag it will be skipped and if it is another sub struct | ||
// it will continue until it reaches the end. | ||
func FetchObjects(str any, builder string) { | ||
b := gtk.NewBuilderFromString(builder) | ||
walk.Into(str, func(v reflect.Value, sf reflect.StructField) bool { | ||
tag, ok := sf.Tag.Lookup("gtk") | ||
if !ok { | ||
return true | ||
} | ||
obj := b.GetObject(tag).Cast() | ||
v.Set(reflect.ValueOf(obj)) | ||
return true | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters