Skip to content

Commit 7a8debc

Browse files
committed
feat(codegen): 添加 GoDefine
1 parent 0370a68 commit 7a8debc

File tree

5 files changed

+105
-9
lines changed

5 files changed

+105
-9
lines changed

.github/workflows/go.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ name: Go
22
on: [push, pull_request]
33

44
jobs:
5-
65
test:
76
name: Test
87
runs-on: ${{ matrix.os }}
98

109
strategy:
1110
matrix:
1211
os: [ubuntu-latest, macOS-latest, windows-latest]
13-
go: ['1.22.x', '1.23.x']
12+
go: ["1.23.x", "1.24.x"]
1413

1514
steps:
16-
1715
- name: Check out code into the Go module directory
1816
uses: actions/checkout@v4
1917

@@ -30,7 +28,7 @@ jobs:
3028
run: go test -v -coverprofile='coverage.txt' -covermode=atomic ./...
3129

3230
- name: Upload Coverage report
33-
uses: codecov/codecov-action@v4
31+
uses: codecov/codecov-action@v5
3432
with:
3533
token: ${{secrets.CODECOV_TOKEN}}
36-
file: ./coverage.txt
34+
files: ./coverage.txt

codegen/object.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// SPDX-FileCopyrightText: 2025 caixw
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
package codegen
6+
7+
import (
8+
"reflect"
9+
"strconv"
10+
"strings"
11+
12+
"github.com/issue9/errwrap"
13+
)
14+
15+
// GoDefine 返回对象 v 在 Go 源码中的定义方式
16+
func GoDefine(t reflect.Type) string {
17+
buf := &errwrap.Buffer{}
18+
goDefine(buf, 0, t)
19+
return buf.String()
20+
}
21+
22+
func goDefine(buf *errwrap.Buffer, indent int, t reflect.Type) {
23+
for t.Kind() == reflect.Pointer {
24+
t = t.Elem()
25+
}
26+
27+
switch t.Kind() {
28+
case reflect.Func, reflect.Chan: // 忽略
29+
case reflect.Slice:
30+
buf.WString("[]").WString(t.Elem().Name())
31+
case reflect.Array:
32+
buf.WByte('[').WString(strconv.Itoa(t.Len())).WByte(']').WString(t.Elem().Name())
33+
case reflect.Struct:
34+
buf.WString("{\n")
35+
indent++
36+
for i := 0; i < t.NumField(); i++ {
37+
f := t.Field(i)
38+
if f.Type.Kind() == reflect.Func || f.Type.Kind() == reflect.Chan {
39+
continue
40+
}
41+
42+
buf.WString(strings.Repeat("\t", indent)).WString(f.Name).WByte('\t')
43+
goDefine(buf, indent, f.Type)
44+
45+
if f.Tag != "" {
46+
buf.WByte('\t').WByte('`').WString(string(f.Tag)).WByte('`')
47+
}
48+
49+
buf.WByte('\n')
50+
}
51+
52+
indent--
53+
buf.WString(strings.Repeat("\t", indent)).WByte('}')
54+
default:
55+
buf.WString(t.Name())
56+
}
57+
}

codegen/object_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-FileCopyrightText: 2025 caixw
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
package codegen
6+
7+
import (
8+
"reflect"
9+
"testing"
10+
11+
"github.com/issue9/assert/v4"
12+
)
13+
14+
type object1 struct {
15+
Int int `json:"int" yaml:"int"`
16+
Array [5]int
17+
Slice []string
18+
Byte byte
19+
Chan chan int
20+
}
21+
22+
type object2 struct {
23+
Int int `json:"int" yaml:"int"`
24+
Object *object1 `json:"object"`
25+
}
26+
27+
func TestGoDefine(t *testing.T) {
28+
a := assert.New(t, false)
29+
30+
wont := "{\n\tInt\tint\t`json:\"int\" yaml:\"int\"`\n\tArray\t[5]int\n\tSlice\t[]string\n\tByte\tuint8\n}"
31+
a.Equal(GoDefine(reflect.TypeFor[object1]()), wont)
32+
33+
wont = "{\n\tInt\tint\t`json:\"int\" yaml:\"int\"`\n\tObject\t{\n\t\tInt\tint\t`json:\"int\" yaml:\"int\"`\n\t\tArray\t[5]int\n\t\tSlice\t[]string\n\t\tByte\tuint8\n\t}\t`json:\"object\"`\n}"
34+
a.Equal(GoDefine(reflect.TypeFor[*object2]()), wont)
35+
36+
a.Equal(GoDefine(reflect.TypeFor[int]()), "int")
37+
38+
a.Equal(GoDefine(reflect.TypeFor[string]()), "string")
39+
40+
a.Equal(GoDefine(reflect.TypeFor[func()]()), "")
41+
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module github.com/issue9/source
22

3-
go 1.22.0
3+
go 1.23.0
44

55
require (
66
github.com/issue9/assert/v4 v4.3.1
77
github.com/issue9/errwrap v0.3.2
8-
golang.org/x/mod v0.22.0
8+
golang.org/x/mod v0.23.0
99
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ github.com/issue9/assert/v4 v4.3.1 h1:dHYODk1yV7j/1baIB6K6UggI4r1Hfuljqic7PaDbwL
22
github.com/issue9/assert/v4 v4.3.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
33
github.com/issue9/errwrap v0.3.2 h1:7KEme9Pfe75M+sIMcPCn/DV90wjnOcRbO4DXVAHj3Fw=
44
github.com/issue9/errwrap v0.3.2/go.mod h1:KcCLuUGiffjooLCUjL89r1cyO8/HT/VRcQrneO53N3A=
5-
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
6-
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
5+
golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM=
6+
golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=

0 commit comments

Comments
 (0)