Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
825 changes: 825 additions & 0 deletions go/fory/codegen/forygen.go

Large diffs are not rendered by default.

108 changes: 108 additions & 0 deletions go/fory/codegen_tests/codegen_tests_fory_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions go/fory/codegen_tests/structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package codegen_tests

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move this test into tests, it's unnecessary to have another test package


// ValidationDemo is a simple struct for testing code generation
// Contains only basic types since PR1 only supports basic types
type ValidationDemo struct {
A int32 `json:"a"` // int32 field
B string `json:"b"` // string field
C int64 `json:"c"` // int64 field (instead of array, as arrays are not supported yet)
}
63 changes: 63 additions & 0 deletions go/fory/codegen_tests/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package codegen_tests

import (
"testing"

"github.com/apache/fory/go/fory"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

//go:generate go run ../codegen/forygen.go -pkg . -type "ValidationDemo"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think users of fory can specify like this. It's OK to have something like this to make debug more easily. But we also should have annotation like go:generate fory ....


func TestValidationDemo(t *testing.T) {
// 1. Create test instance
original := &ValidationDemo{
A: 12345, // int32
B: "Hello Fory!", // string
C: 98765, // int64
}

// Validate original data structure
assert.Equal(t, int32(12345), original.A, "Original A should be 12345")
assert.Equal(t, "Hello Fory!", original.B, "Original B should be 'Hello Fory!'")
assert.Equal(t, int64(98765), original.C, "Original C should be 98765")

// 2. Serialize using generated code
f := fory.NewFory(true)
data, err := f.Marshal(original)
require.NoError(t, err, "Serialization should not fail")
require.NotEmpty(t, data, "Serialized data should not be empty")
assert.Greater(t, len(data), 0, "Serialized data should have positive length")

// 3. Deserialize using generated code
var result *ValidationDemo
err = f.Unmarshal(data, &result)
require.NoError(t, err, "Deserialization should not fail")
require.NotNil(t, result, "Deserialized result should not be nil")

// 4. Validate round-trip serialization
assert.Equal(t, original.A, result.A, "Field A should match after round-trip")
assert.Equal(t, original.B, result.B, "Field B should match after round-trip")
assert.Equal(t, original.C, result.C, "Field C should match after round-trip")

// 5. Validate data integrity
assert.EqualValues(t, original, result, "Complete struct should match after round-trip")
}
25 changes: 25 additions & 0 deletions go/fory/fory.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ import (
)

func NewFory(referenceTracking bool) *Fory {
fory := &Fory{
refResolver: newRefResolver(referenceTracking),
referenceTracking: referenceTracking,
language: XLANG,
buffer: NewByteBuffer(nil),
}
// Create a new type resolver for this instance but copy generated serializers from global resolver
fory.typeResolver = newTypeResolver(fory)

// Copy generated serializers from global resolver to this instance
if globalTypeResolver != nil {
for typ, serializer := range globalTypeResolver.typeToSerializers {
fory.typeResolver.typeToSerializers[typ] = serializer
}
for typeId, typ := range globalTypeResolver.typeIdToType {
fory.typeResolver.typeIdToType[typeId] = typ
}
}

return fory
}

// NewForyWithIsolatedTypes creates a Fory instance with isolated type resolver
// for use cases that need independent type registration
func NewForyWithIsolatedTypes(referenceTracking bool) *Fory {
fory := &Fory{
refResolver: newRefResolver(referenceTracking),
referenceTracking: referenceTracking,
Expand Down
3 changes: 2 additions & 1 deletion go/fory/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ go 1.13
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.6.1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that lowering the version makes no sense

golang.org/x/tools v0.1.12
)
30 changes: 28 additions & 2 deletions go/fory/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,34 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
12 changes: 12 additions & 0 deletions go/fory/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,15 @@ func readUTF8(buf *ByteBuffer, size int) string {
data := buf.ReadBinary(size)
return string(data) // Direct UTF-8 conversion
}

// WriteString provides public API for zero-reflection string serialization
// This method is specifically designed for code generation to avoid reflection overhead
func WriteString(buf *ByteBuffer, value string) error {
return writeString(buf, value)
}

// ReadString provides public API for zero-reflection string deserialization
// This method is specifically designed for code generation to avoid reflection overhead
func ReadString(buf *ByteBuffer) string {
return readString(buf)
}
Loading
Loading