-
Notifications
You must be signed in to change notification settings - Fork 412
feat(dart): Introduce id based enum serialization #3482
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
Open
yash-agarwa-l
wants to merge
8
commits into
apache:main
Choose a base branch
from
yash-agarwa-l:id-based-enum
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a656042
feat(fory_enum): Add Target to ForyEnum
yash-agarwa-l c7deff7
a seperate annotation for fory_enum_id introduced
yash-agarwa-l 9dd2acb
add foryEnumId to indentifiers just next foryEnum
yash-agarwa-l 2cab4ae
analyze and pass it to spec
yash-agarwa-l 3307be0
warning added for duplicate ids or no id at some fields
yash-agarwa-l b4a1551
removed todo
yash-agarwa-l daa62fb
update EnumSerializer and test enums
yash-agarwa-l 1c56dfe
add tests
yash-agarwa-l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| import 'package:fory/fory.dart'; | ||
|
|
||
| part '../generated/enum_id_foo.g.dart'; | ||
|
|
||
| @foryEnum | ||
| enum EnumWithIds { | ||
| @ForyEnumId(10) | ||
| A, | ||
| @ForyEnumId(20) | ||
| B, | ||
| @ForyEnumId(30) | ||
| C, | ||
| } | ||
|
|
||
| @foryEnum | ||
| enum EnumPartialIds { | ||
| @ForyEnumId(10) | ||
| A, | ||
| B, | ||
| @ForyEnumId(30) | ||
| C, | ||
| } | ||
|
|
||
| @foryEnum | ||
| enum EnumDuplicateIds { | ||
| @ForyEnumId(10) | ||
| A, | ||
| @ForyEnumId(10) | ||
| B, | ||
| @ForyEnumId(30) | ||
| C, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
dart/packages/fory-test/test/datatype_test/enum_serializer_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| library; | ||
|
|
||
| import 'package:checks/checks.dart'; | ||
| import 'package:fory/src/collection/stack.dart'; | ||
| import 'package:fory/src/config/fory_config.dart'; | ||
| import 'package:fory/src/deserialization_dispatcher.dart'; | ||
| import 'package:fory/src/deserialization_context.dart'; | ||
| import 'package:fory/src/fory_exception.dart'; | ||
| import 'package:fory/src/memory/byte_reader.dart'; | ||
| import 'package:fory/src/memory/byte_writer.dart'; | ||
| import 'package:fory/src/meta/spec_wraps/type_spec_wrap.dart'; | ||
| import 'package:fory/src/resolver/deserialization_ref_resolver.dart'; | ||
| import 'package:fory/src/resolver/meta_string_writing_resolver.dart'; | ||
| import 'package:fory/src/resolver/serialization_ref_resolver.dart'; | ||
| import 'package:fory/src/resolver/struct_hash_resolver.dart'; | ||
| import 'package:fory/src/resolver/type_resolver.dart'; | ||
| import 'package:fory/src/serialization_dispatcher.dart'; | ||
| import 'package:fory/src/serialization_context.dart'; | ||
| import 'package:fory/src/serializer/enum_serializer.dart'; | ||
| import 'package:fory_test/entity/enum_id_foo.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| String _unusedTagLookup(Type _) => ''; | ||
|
|
||
| final ForyConfig _config = ForyConfig(); | ||
| final TypeResolver _typeResolver = TypeResolver.newOne(_config); | ||
|
|
||
| SerializationContext _newSerializationContext() { | ||
| return SerializationContext( | ||
| StructHashResolver.inst, | ||
| _unusedTagLookup, | ||
| SerializationDispatcher.I, | ||
| _typeResolver, | ||
| SerializationRefResolver.getOne(false), | ||
| SerializationRefResolver.noRefResolver, | ||
| MetaStringWritingResolver.newInst, | ||
| Stack<TypeSpecWrap>(), | ||
| ); | ||
| } | ||
|
|
||
| DeserializationContext _newDeserializationContext() { | ||
| return DeserializationContext( | ||
| StructHashResolver.inst, | ||
| _unusedTagLookup, | ||
| _config, | ||
| (isXLang: true, oobEnabled: false), | ||
| DeserializationDispatcher.I, | ||
| DeserializationRefResolver.getOne(false), | ||
| _typeResolver, | ||
| Stack<TypeSpecWrap>(), | ||
| ); | ||
| } | ||
|
|
||
| void main() { | ||
| group('Enum serializer', () { | ||
| test('writes and reads annotated enum ids when all values are annotated', | ||
| () { | ||
| final EnumSerializer serializer = EnumSerializer(false, [ | ||
| EnumWithIds.A, | ||
| EnumWithIds.B, | ||
| EnumWithIds.C, | ||
| ], { | ||
| 10: EnumWithIds.A, | ||
| 20: EnumWithIds.B, | ||
| 30: EnumWithIds.C, | ||
| }); | ||
|
|
||
| final ByteWriter writer = ByteWriter(); | ||
| serializer.write( | ||
| writer, | ||
| EnumWithIds.B, | ||
| _newSerializationContext(), | ||
| ); | ||
| final ByteReader encodedIdReader = | ||
| ByteReader.forBytes(writer.takeBytes()); | ||
| check(encodedIdReader.readVarUint32Small7()).equals(20); | ||
|
|
||
| final ByteWriter idWriter = ByteWriter(); | ||
| idWriter.writeVarUint32Small7(30); | ||
| final Enum value = serializer.read( | ||
| ByteReader.forBytes(idWriter.takeBytes()), | ||
| 0, | ||
| _newDeserializationContext(), | ||
| ); | ||
| check(value).equals(EnumWithIds.C); | ||
| }); | ||
|
|
||
| test('falls back to ordinal serialization when id mapping is absent', () { | ||
| final EnumSerializer serializer = EnumSerializer(false, [ | ||
| EnumPartialIds.A, | ||
| EnumPartialIds.B, | ||
| EnumPartialIds.C, | ||
| ]); | ||
|
|
||
| final ByteWriter writer = ByteWriter(); | ||
| serializer.write( | ||
| writer, | ||
| EnumPartialIds.B, | ||
| _newSerializationContext(), | ||
| ); | ||
| final ByteReader encodedIdReader = | ||
| ByteReader.forBytes(writer.takeBytes()); | ||
| check(encodedIdReader.readVarUint32Small7()).equals(1); | ||
| }); | ||
|
|
||
| test('throws on unknown annotated enum id', () { | ||
| final EnumSerializer serializer = EnumSerializer(false, [ | ||
| EnumWithIds.A, | ||
| EnumWithIds.B, | ||
| EnumWithIds.C, | ||
| ], { | ||
| 10: EnumWithIds.A, | ||
| 20: EnumWithIds.B, | ||
| 30: EnumWithIds.C, | ||
| }); | ||
|
|
||
| final ByteWriter writer = ByteWriter(); | ||
| writer.writeVarUint32Small7(99); | ||
|
|
||
| check( | ||
| () => serializer.read( | ||
| ByteReader.forBytes(writer.takeBytes()), | ||
| 0, | ||
| _newDeserializationContext(), | ||
| ), | ||
| ).throws<DeserializationRangeException>(); | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
use uppercase?
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.
Yes, we should do it as per conventions, but the existing files also follow the same pattern. If you prefer, shall I make a separate PR for that first before this one?