Skip to content

Commit ab0fd97

Browse files
authored
38 emit golang (#130)
* Introduce maven daemon * Add go output file and impls * Add go client test * Update document
1 parent 77ea11d commit ab0fd97

60 files changed

Lines changed: 1012 additions & 81 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ jobs:
111111
working-directory: client-test/rust
112112
run: cargo test
113113

114+
- uses: actions/setup-go@v5
115+
with:
116+
go-version: 1.24
117+
- name: Test Golang
118+
working-directory: client-test/go
119+
run: go get && go test
120+
114121
# clean-artifact:
115122
# needs: [ jdk_compatibility_test, client_test ]
116123
# runs-on: ubuntu-latest

.mvn/mvnd.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mvnd.noBuffering=true
2+
mvnd.threads=1

.run/mvnDebug.run.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="mvnDebug" type="Remote">
3+
<module name="sharedtype-ap" />
4+
<option name="USE_SOCKET_TRANSPORT" value="true" />
5+
<option name="SERVER_MODE" value="false" />
6+
<option name="SHMEM_ADDRESS" />
7+
<option name="HOST" value="localhost" />
8+
<option name="PORT" value="5005" />
9+
<option name="AUTO_RESTART" value="false" />
10+
<RunnerSettings RunnerId="Debug">
11+
<option name="DEBUG_PORT" value="5005" />
12+
<option name="LOCAL" value="false" />
13+
</RunnerSettings>
14+
<method v="2" />
15+
</configuration>
16+
</component>

.run/mvnd.run.xml

Lines changed: 0 additions & 16 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface User {
1818
email: string;
1919
}
2020
```
21-
Go (Planed):
21+
Go:
2222
```golang
2323
type User struct {
2424
Name string

annotation/src/main/java/online/sharedtype/SharedType.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@
5959
* <p>
6060
* <b>Cyclic Reference:</b>
6161
* <ul>
62-
* <li>Typescript: Cyclic referenced field will be optional.</li>
63-
* <li>Rust: Cyclic references will be wrapped in {@code Option<Box<T>>}.</li>
62+
* <li>Typescript: optional.</li>
63+
* <li>Go: pointer.</li>
64+
* <li>Rust: wrapped in {@code Option<Box<T>>}.</li>
6465
* </ul>
6566
*
6667
* <p>
6768
* <b>Enums:</b><br>
6869
* Enums are emitted as below:
6970
* <ul>
7071
* <li>Typescript: type union or enum. For simple enums, values are enum constants' names.</li>
72+
* <li>Go: const (no namespace, so potential name conflict across enums) or var struct (namespace suffixed with 'Enums'). </li>
7173
* <li>Rust: plain enum for simple enums; impl a const fun {@code value()} for custom enum values.</li>
7274
* </ul>
7375
* See {@link EnumValue} for how to mark an enum value.
@@ -95,6 +97,7 @@
9597
* Iterables and arrays are treated as arrays and, by default, are mapped to:
9698
* <ul>
9799
* <li>Typescript: {@code T[]}</li>
100+
* <li>Go: {@code []T}</li>
98101
* <li>Rust: {@code Vec<T>}</li>
99102
* </ul>
100103
*
@@ -106,6 +109,7 @@
106109
* E.g. a type {@code Optional<Optional<List<Optional<String>>>} can be emitted as:
107110
* <ul>
108111
* <li>Typescript: {@code String[] | undefined}</li>
112+
* <li>Go: pointers</li>
109113
* <li>Rust: {@code Option<Vec<String>>}</li>
110114
* </ul>
111115
*
@@ -115,6 +119,7 @@
115119
* Custom map types are supported, e.g. a class that extends HashMap. But the type itself is treated as a mapType, so its structure will not be emitted.
116120
* <ul>
117121
* <li>Typescript: e.g. {@code Record<string, T>} where {@code T} can be a reified type. If the key is enum, it will be a {@code Partial<Record<?, ?>>}</li>
122+
* <li>Go: e.g. {@code map[string]T}</li>
118123
* <li>Rust: e.g. {@code HashMap<String, T>}, {@code HashMap<EnumType, T>}</li>
119124
* </ul>
120125
*
@@ -250,6 +255,18 @@
250255
*/
251256
String typescriptTargetDatetimeTypeLiteral() default "";
252257

258+
/**
259+
* Type literal to be emitted for date/time types. How a java type is considered a date/time type is defined by global properties.
260+
* @return any literal, e.g. "string", "Date". When empty, fallback to global default.
261+
*/
262+
String goTargetDatetimeTypeLiteral() default "";
263+
264+
/**
265+
* Format of enum in Go.
266+
* @return "const" or "struct". If empty, fallback to global default.
267+
*/
268+
String goEnumFormat() default "";
269+
253270
/**
254271
* Mark a method as an accessor regardless of its name.
255272
* Getter prefixes are configured in global properties.

client-test/go/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module sharedtype.online/go-client-test
2+
3+
go 1.23.2
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/pmezard/go-difflib v1.0.0 // indirect
8+
github.com/stretchr/testify v1.10.0 // indirect
9+
gopkg.in/yaml.v3 v3.0.1 // indirect
10+
)

client-test/go/go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
6+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
9+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

client-test/go/main.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package sharedtype
2+
3+
var List1 = []EnumGalaxy{"Andromeda", "MilkyWay", "Triangulum"}
4+
var Record1 = map[EnumTShirt]int32{
5+
S: 1,
6+
M: 2,
7+
L: 3,
8+
}
9+
var Size1 EnumSize = 1
10+
var TshirtSize1 EnumTShirt = S
11+
12+
var dependencyClassC1 = &DependencyClassC{}
13+
14+
var dependencyClassB1 = &DependencyClassB{
15+
C: dependencyClassC1,
16+
}
17+
18+
var dependencyClassA1 = &DependencyClassA{
19+
SuperClassA: SuperClassA{
20+
A: 0,
21+
NotIgnoredImplementedMethod: 0,
22+
Value: 0,
23+
},
24+
B: dependencyClassB1,
25+
}
26+
27+
var Obj = JavaRecord[string]{
28+
BoxedBoolean: false,
29+
BoxedByte: 0,
30+
BoxedChar: 'a',
31+
BoxedDouble: 0,
32+
BoxedFloat: 0,
33+
BoxedInt: 0,
34+
BoxedIntArray: []int32{},
35+
BoxedLong: 0,
36+
BoxedShort: 0,
37+
ContainerStringList: []Container[string]{},
38+
ContainerStringListCollection: [][]Container[string]{},
39+
CyclicDependency: dependencyClassA1,
40+
DuplicateAccessor: "",
41+
EnumGalaxy: "MilkyWay",
42+
EnumSize: 3,
43+
GenericList: []string{},
44+
GenericListSet: [][]string{},
45+
GenericSet: []string{},
46+
IntArray: []int32{},
47+
Object: nil,
48+
PrimitiveBoolean: false,
49+
PrimitiveByte: 0,
50+
PrimitiveChar: 'b',
51+
PrimitiveDouble: 0,
52+
PrimitiveFloat: 0,
53+
PrimitiveInt: 0,
54+
PrimitiveLong: 0,
55+
PrimitiveShort: 0,
56+
String: "",
57+
Value: "",
58+
}
59+
60+
var AnotherJavaClass1 = &AnotherJavaClass{
61+
Value: 333,
62+
}
63+
64+
var RecursiveClass1 = &RecursiveClass{
65+
DirectRef: &RecursiveClass{
66+
DirectRef: nil,
67+
ArrayRef: []RecursiveClass{},
68+
},
69+
ArrayRef: []RecursiveClass{},
70+
}
71+
72+
var MapClass1 = MapClass{
73+
MapField: map[int32]string{},
74+
EnumKeyMapField: map[EnumSize]string{
75+
1: "1",
76+
},
77+
CustomMapField: map[int32]string{
78+
55: "abc",
79+
},
80+
NestedMapField: map[string]map[string]int32{
81+
"M1": {
82+
"V": 1,
83+
},
84+
},
85+
}
86+
87+
var OptionalMethods1 = OptionalMethod{
88+
ValueOptional: nil,
89+
NestedValueOptional: nil,
90+
SetNestedValueOptional: nil,
91+
MapNestedValueOptional: nil,
92+
}
93+
94+
var ArrayClass1 = ArrayClass{
95+
Arr: []string{"abc"},
96+
}
97+
98+
var JavaTime1 = JavaTimeClass{
99+
UtilDate: "2022-01-01T00:00:00.000+08:00",
100+
SqlDate: "2022-01-01T00:00:00.000+08:00",
101+
LocalDate: "2022-01-01T00:00:00.000+08:00",
102+
LocalTime: "2022-01-01T00:00:00.000+08:00",
103+
LocalDateTime: "2022-01-01T00:00:00.000+08:00",
104+
ZonedDateTime: "2022-01-01T00:00:00.000+08:00",
105+
OffsetDateTime: "2022-01-01T00:00:00.000+08:00",
106+
OffsetTime: "2022-01-01T00:00:00.000+08:00",
107+
Instant: "2022-01-01T00:00:00.000+08:00",
108+
}
109+
110+
var JodaTime1 = JodaTimeClass{
111+
JodaDateTime: "2022-01-01T00:00:00.000+08:00",
112+
JodaLocalDate: "2022-01-01T00:00:00.000+08:00",
113+
JodaMonthDay: "2022-01-01T00:00:00.000+08:00",
114+
JodaLocalTime: "2022-01-01T00:00:00.000+08:00",
115+
JodaLocalDateTime: "2022-01-01T00:00:00.000+08:00",
116+
JodaOffsetDateTime: "2022-01-01T00:00:00.000+08:00",
117+
}
118+
119+
var MathClass1 = MathClass{
120+
BigDecimal: "1.1",
121+
BigInteger: "8888888555555",
122+
}
123+
124+
var EnumConstValue1 EnumConstReference = 156
125+
var EnumEnumValue1 EnumEnumReference = 3

client-test/go/main_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package sharedtype
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestMain(t *testing.T) {
10+
dependencyClassC1.A = dependencyClassA1
11+
12+
RecursiveClass1.DirectRef = &RecursiveClass{
13+
DirectRef: nil,
14+
ArrayRef: []RecursiveClass{},
15+
}
16+
17+
OptionalMethods1.MapNestedValueOptional = &map[int32]string{1: "foo"}
18+
OptionalMethods1.SetNestedValueOptional = &[]string{"foo", "bar"}
19+
OptionalMethods1.NestedValueOptional = &List1[0]
20+
}
21+
22+
func TestConstants(t *testing.T) {
23+
assert.Equal(t, float32(1.888), FLOAT_VALUE)
24+
assert.Equal(t, "MilkyWay", REFERENCED_ENUM_VALUE)
25+
assert.Equal(t, int32(1), MyEnumConstants.INT_VALUE)
26+
}

0 commit comments

Comments
 (0)