-
Notifications
You must be signed in to change notification settings - Fork 439
feat(Go): Implement ahead of time codegen for fory-go serialization #2553
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
Changes from 6 commits
c36dfb6
665c690
487dcfc
f0e8212
4a9a545
6c91a77
9daea49
9ab75a4
53e6756
bfe7816
dbef1c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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 | ||
|
|
||
| // 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) | ||
| } | ||
| 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" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| 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") | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ) | ||
There was a problem hiding this comment.
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