Skip to content

38 emit golang #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ jobs:
working-directory: client-test/rust
run: cargo test

- uses: actions/setup-go@v5
with:
go-version: 1.24
- name: Test Golang
working-directory: client-test/go
run: go get && go test

# clean-artifact:
# needs: [ jdk_compatibility_test, client_test ]
# runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .mvn/mvnd.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mvnd.noBuffering=true
mvnd.threads=1
16 changes: 16 additions & 0 deletions .run/mvnDebug.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="mvnDebug" type="Remote">
<module name="sharedtype-ap" />
<option name="USE_SOCKET_TRANSPORT" value="true" />
<option name="SERVER_MODE" value="false" />
<option name="SHMEM_ADDRESS" />
<option name="HOST" value="localhost" />
<option name="PORT" value="5005" />
<option name="AUTO_RESTART" value="false" />
<RunnerSettings RunnerId="Debug">
<option name="DEBUG_PORT" value="5005" />
<option name="LOCAL" value="false" />
</RunnerSettings>
<method v="2" />
</configuration>
</component>
16 changes: 0 additions & 16 deletions .run/mvnd.run.xml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface User {
email: string;
}
```
Go (Planed):
Go:
```golang
type User struct {
Name string
Expand Down
21 changes: 19 additions & 2 deletions annotation/src/main/java/online/sharedtype/SharedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@
* <p>
* <b>Cyclic Reference:</b>
* <ul>
* <li>Typescript: Cyclic referenced field will be optional.</li>
* <li>Rust: Cyclic references will be wrapped in {@code Option<Box<T>>}.</li>
* <li>Typescript: optional.</li>
* <li>Go: pointer.</li>
* <li>Rust: wrapped in {@code Option<Box<T>>}.</li>
* </ul>
*
* <p>
* <b>Enums:</b><br>
* Enums are emitted as below:
* <ul>
* <li>Typescript: type union or enum. For simple enums, values are enum constants' names.</li>
* <li>Go: const (no namespace, so potential name conflict across enums) or var struct (namespace suffixed with 'Enums'). </li>
* <li>Rust: plain enum for simple enums; impl a const fun {@code value()} for custom enum values.</li>
* </ul>
* See {@link EnumValue} for how to mark an enum value.
Expand Down Expand Up @@ -95,6 +97,7 @@
* Iterables and arrays are treated as arrays and, by default, are mapped to:
* <ul>
* <li>Typescript: {@code T[]}</li>
* <li>Go: {@code []T}</li>
* <li>Rust: {@code Vec<T>}</li>
* </ul>
*
Expand All @@ -106,6 +109,7 @@
* E.g. a type {@code Optional<Optional<List<Optional<String>>>} can be emitted as:
* <ul>
* <li>Typescript: {@code String[] | undefined}</li>
* <li>Go: pointers</li>
* <li>Rust: {@code Option<Vec<String>>}</li>
* </ul>
*
Expand All @@ -115,6 +119,7 @@
* 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.
* <ul>
* <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>
* <li>Go: e.g. {@code map[string]T}</li>
* <li>Rust: e.g. {@code HashMap<String, T>}, {@code HashMap<EnumType, T>}</li>
* </ul>
*
Expand Down Expand Up @@ -250,6 +255,18 @@
*/
String typescriptTargetDatetimeTypeLiteral() default "";

/**
* Type literal to be emitted for date/time types. How a java type is considered a date/time type is defined by global properties.
* @return any literal, e.g. "string", "Date". When empty, fallback to global default.
*/
String goTargetDatetimeTypeLiteral() default "";

/**
* Format of enum in Go.
* @return "const" or "struct". If empty, fallback to global default.
*/
String goEnumFormat() default "";

/**
* Mark a method as an accessor regardless of its name.
* Getter prefixes are configured in global properties.
Expand Down
10 changes: 10 additions & 0 deletions client-test/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module sharedtype.online/go-client-test

go 1.23.2

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.10.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions client-test/go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
125 changes: 125 additions & 0 deletions client-test/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package sharedtype

var List1 = []EnumGalaxy{"Andromeda", "MilkyWay", "Triangulum"}
var Record1 = map[EnumTShirt]int32{
S: 1,
M: 2,
L: 3,
}
var Size1 EnumSize = 1
var TshirtSize1 EnumTShirt = S

var dependencyClassC1 = &DependencyClassC{}

var dependencyClassB1 = &DependencyClassB{
C: dependencyClassC1,
}

var dependencyClassA1 = &DependencyClassA{
SuperClassA: SuperClassA{
A: 0,
NotIgnoredImplementedMethod: 0,
Value: 0,
},
B: dependencyClassB1,
}

var Obj = JavaRecord[string]{
BoxedBoolean: false,
BoxedByte: 0,
BoxedChar: 'a',
BoxedDouble: 0,
BoxedFloat: 0,
BoxedInt: 0,
BoxedIntArray: []int32{},
BoxedLong: 0,
BoxedShort: 0,
ContainerStringList: []Container[string]{},
ContainerStringListCollection: [][]Container[string]{},
CyclicDependency: dependencyClassA1,
DuplicateAccessor: "",
EnumGalaxy: "MilkyWay",
EnumSize: 3,
GenericList: []string{},
GenericListSet: [][]string{},
GenericSet: []string{},
IntArray: []int32{},
Object: nil,
PrimitiveBoolean: false,
PrimitiveByte: 0,
PrimitiveChar: 'b',
PrimitiveDouble: 0,
PrimitiveFloat: 0,
PrimitiveInt: 0,
PrimitiveLong: 0,
PrimitiveShort: 0,
String: "",
Value: "",
}

var AnotherJavaClass1 = &AnotherJavaClass{
Value: 333,
}

var RecursiveClass1 = &RecursiveClass{
DirectRef: &RecursiveClass{
DirectRef: nil,
ArrayRef: []RecursiveClass{},
},
ArrayRef: []RecursiveClass{},
}

var MapClass1 = MapClass{
MapField: map[int32]string{},
EnumKeyMapField: map[EnumSize]string{
1: "1",
},
CustomMapField: map[int32]string{
55: "abc",
},
NestedMapField: map[string]map[string]int32{
"M1": {
"V": 1,
},
},
}

var OptionalMethods1 = OptionalMethod{
ValueOptional: nil,
NestedValueOptional: nil,
SetNestedValueOptional: nil,
MapNestedValueOptional: nil,
}

var ArrayClass1 = ArrayClass{
Arr: []string{"abc"},
}

var JavaTime1 = JavaTimeClass{
UtilDate: "2022-01-01T00:00:00.000+08:00",
SqlDate: "2022-01-01T00:00:00.000+08:00",
LocalDate: "2022-01-01T00:00:00.000+08:00",
LocalTime: "2022-01-01T00:00:00.000+08:00",
LocalDateTime: "2022-01-01T00:00:00.000+08:00",
ZonedDateTime: "2022-01-01T00:00:00.000+08:00",
OffsetDateTime: "2022-01-01T00:00:00.000+08:00",
OffsetTime: "2022-01-01T00:00:00.000+08:00",
Instant: "2022-01-01T00:00:00.000+08:00",
}

var JodaTime1 = JodaTimeClass{
JodaDateTime: "2022-01-01T00:00:00.000+08:00",
JodaLocalDate: "2022-01-01T00:00:00.000+08:00",
JodaMonthDay: "2022-01-01T00:00:00.000+08:00",
JodaLocalTime: "2022-01-01T00:00:00.000+08:00",
JodaLocalDateTime: "2022-01-01T00:00:00.000+08:00",
JodaOffsetDateTime: "2022-01-01T00:00:00.000+08:00",
}

var MathClass1 = MathClass{
BigDecimal: "1.1",
BigInteger: "8888888555555",
}

var EnumConstValue1 EnumConstReference = 156
var EnumEnumValue1 EnumEnumReference = 3
26 changes: 26 additions & 0 deletions client-test/go/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sharedtype

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMain(t *testing.T) {
dependencyClassC1.A = dependencyClassA1

RecursiveClass1.DirectRef = &RecursiveClass{
DirectRef: nil,
ArrayRef: []RecursiveClass{},
}

OptionalMethods1.MapNestedValueOptional = &map[int32]string{1: "foo"}
OptionalMethods1.SetNestedValueOptional = &[]string{"foo", "bar"}
OptionalMethods1.NestedValueOptional = &List1[0]
}

func TestConstants(t *testing.T) {
assert.Equal(t, float32(1.888), FLOAT_VALUE)
assert.Equal(t, "MilkyWay", REFERENCED_ENUM_VALUE)
assert.Equal(t, int32(1), MyEnumConstants.INT_VALUE)
}
5 changes: 5 additions & 0 deletions client-test/go/setenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

export PATH=$GO_HOME/bin:$PATH
go version

1 change: 1 addition & 0 deletions client-test/go/types.go
2 changes: 1 addition & 1 deletion client-test/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ mod tests {
assert_eq!(EnumConstReference::ReferenceConstantInOther.value(), 999);
assert_eq!(EnumConstReference::ReferenceConstantLocally.value(), 156);
assert_eq!(EnumEnumReference::ReferenceAnother.value(), 3);
assert_eq!(EnumSimpleEnumReference::ReferenceAnother.value(), EnumGalaxy::Andromeda);
assert_eq!(EnumSimpleEnumReference::ReferenceAnother1.value(), EnumGalaxy::Andromeda);
assert_eq!(EnumEnumEnumReference::ReferenceAnother2.value(), EnumGalaxy::Andromeda);
}
}
5 changes: 3 additions & 2 deletions doc/Development.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Optionally mount tmpfs to save your disk by:
```bash
./mount-tmpfs.sh
```
Optionally setup `MVND_HOME` to use [maven daemon](https://github.com/apache/maven-mvnd) by `./mvnd`

### Maven profiles
* `dev` and `release` - control whether to include test maven modules during build.
Expand Down Expand Up @@ -63,9 +64,9 @@ Style check:
```
Debug annotation processor by run maven build:
```bash
./mvnd <your args goes here>
./mvne <your args goes here>
```
Then attach your debugger on it. E.g. [IDEA run config](../.run/mvnd.run.xml).
Then attach your debugger on it. E.g. [IDEA run config](../.run/mvnDebug.run.xml).

Compile specific classes, **along with debug, this is useful for developing a specific feature**, e.g.:
```bash
Expand Down
2 changes: 2 additions & 0 deletions doc/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,5 @@ You have to execute the annotation processing on the same classpath with source
For multiple module builds, a workaround is to execute on every module.
* Non-static inner classes are not supported. Instance class may refer to its enclosing class's generic type without the type declaration on its own,
which could break the generated code. Later version of SharedType may loosen this limitation.
* Although SharedType does not depend on any serialization format, it assumes the contract of JSON.
That means a client can use any format of serializations, but may not exceed JSON capacity.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void parseEnumSimpleEnumReference() {

EnumValueInfo constant1 = enumDef.components().get(0);
assertThat(constant1.value().getValue()).isEqualTo("Andromeda");
assertThat(constant1.value().getEnumConstantName()).isEqualTo("ReferenceAnother");
assertThat(constant1.value().getEnumConstantName()).isEqualTo("ReferenceAnother1");
var constant1TypeInfo = (ConcreteTypeInfo)constant1.value().getValueType();
assertThat(constant1TypeInfo.qualifiedName()).isEqualTo("online.sharedtype.it.java8.EnumGalaxy");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ enum EnumEnumReference {
@RequiredArgsConstructor
@SharedType
enum EnumSimpleEnumReference {
ReferenceAnother(EnumGalaxy.Andromeda),
ReferenceAnother1(EnumGalaxy.Andromeda),
;
@SharedType.EnumValue
private final EnumGalaxy enumValue;
Expand All @@ -42,7 +42,7 @@ enum EnumSimpleEnumReference {
@RequiredArgsConstructor
@SharedType
enum EnumEnumEnumReference {
ReferenceAnother2(EnumSimpleEnumReference.ReferenceAnother),
ReferenceAnother2(EnumSimpleEnumReference.ReferenceAnother1),
;
@SharedType.EnumValue
private final EnumSimpleEnumReference enumValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

@SharedType(
rustMacroTraits = {"PartialEq", "Eq", "Hash", "serde::Serialize", "serde::Deserialize"},
typescriptEnumFormat = "const_enum"
typescriptEnumFormat = "const_enum",
goEnumFormat = "struct"
)
@RequiredArgsConstructor
public enum EnumSize {
Expand Down
4 changes: 3 additions & 1 deletion it/sharedtype.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sharedtype.targets=CONSOLE, JAVA_SERIALIZED, TYPESCRIPT, RUST
sharedtype.targets=CONSOLE, JAVA_SERIALIZED, TYPESCRIPT, RUST, GO
sharedtype.ignore-annotations=online.sharedtype.it.java8.anno.ToIgnore
sharedtype.accessor-annotations=online.sharedtype.it.java8.anno.AsAccessor
sharedtype.enum-value-annotations=online.sharedtype.it.java8.anno.AsEnumValue
Expand All @@ -8,3 +8,5 @@ sharedtype.typescript.type-mappings=online.sharedtype.it.java8.types.MyType1:Arr
online.sharedtype.it.java8.types.MyType2:number

sharedtype.rust.type-mappings=online.sharedtype.it.java8.types.MyType1:Vec

sharedtype.go.type-mappings=online.sharedtype.it.java8.types.MyType1:[]
4 changes: 0 additions & 4 deletions mvnd

This file was deleted.

Loading
Loading