Skip to content

Dart object API#6682

Merged
aardappel merged 14 commits into
google:masterfrom
vaind:dart-object-api
Jun 14, 2021
Merged

Dart object API#6682
aardappel merged 14 commits into
google:masterfrom
vaind:dart-object-api

Conversation

@vaind
Copy link
Copy Markdown
Contributor

@vaind vaind commented Jun 5, 2021

closes #6678

@github-actions github-actions Bot added c++ codegen Involving generating code from schema dart labels Jun 5, 2021
@aardappel aardappel requested a review from dnfield June 7, 2021 15:55
Copy link
Copy Markdown
Collaborator

@aardappel aardappel left a comment

Choose a reason for hiding this comment

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

Generally looks good!
Made some initial comments, hopefully @dnfield can tell us if this looks good from an idiomatic Dart perspective.

Comment thread dart/test/monster_test_my_game.example2_generated.dart Outdated

int _pack(fb.Builder fbBuilder, Map<Object, int> existingOffsets) {
assert(existingOffsets != null);
assert(fbBuilder != null);
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.

this seems a bit superfluous in generated code.. most callers are other bits of generated code, and the user calling it with a null builder doesn't seem very likely.

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.

FWIW, these are stripped away in non-debug modes (and can be stripped away even in debug mode).

However, if it's likely that this indicates an error in the generated code rather than in the caller, it'd be good to at least provide a message to that effect. This also can go away whenever we get to migrate this code to NNBD, which I think someone is working on.

Copy link
Copy Markdown
Contributor Author

@vaind vaind Jun 8, 2021

Choose a reason for hiding this comment

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

Again as mentioned above - keeping in line with the previous ObjectBuilder code. Also, it doesn't hurt anything and is really only a temporary piece of code to be removed with null-safety.

That "someone" working on null-safety (or rather porting it from my work in the ObjectBox fork of flatbuffers) is me, at least I've said I would but didn't get to it yet. But it is on my list soon-ish, hopefully this PR will be merged by the time I do that, to avoid code conflicts.


fbBuilder.pad(2);
test3.pack(fbBuilder);
fbBuilder.pad(1);
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.

the fact that its manually padding to me means that it is serializing manually rather than use the methods from the base API to serialize which presumably already pad for you? better to use those?

Copy link
Copy Markdown
Contributor Author

@vaind vaind Jun 8, 2021

Choose a reason for hiding this comment

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

Yet again, code from the existing ObjectBuilder.finish() - should - do you want to touch that, i.e. should I have a look why it doesn't use some existing method there?

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.

well, we shouldn't copy existing code from the base API, we should be calling into it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is existing generated code, not FlatBuffers API, and unfortunately can't be reused directly (except on the generator side, where I do it) because of a couple of differences, like how inner tables/structs are provided. I can do that but I'd probably have to change some of the generated code unrelated to ObjectAPIs - is that how you'd prefer to do this instead?

final int idOffset = fbBuilder.writeString(id);

fbBuilder.startTable();
if (idOffset != null) {
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.

compares integer with null ?

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.

ints are nullable in dart, unless you're in null-safety mode in which case it'd be written as int?.

I don't think we should ever expect writeString to return null - for example if the buffer is out of space wouldn't we just throw?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

writeString() returns null if the given string (value) is null itself.

Comment thread dart/test/monster_test_my_game.example_generated.dart
Comment thread dart/test/monster_test_my_game.example2_generated.dart Outdated

int _pack(fb.Builder fbBuilder, Map<Object, int> existingOffsets) {
assert(existingOffsets != null);
assert(fbBuilder != null);
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.

FWIW, these are stripped away in non-debug modes (and can be stripped away even in debug mode).

However, if it's likely that this indicates an error in the generated code rather than in the caller, it'd be good to at least provide a message to that effect. This also can go away whenever we get to migrate this code to NNBD, which I think someone is working on.

Comment on lines +58 to +59
fbBuilder.startTable();
return fbBuilder.endTable();
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.

Why does this just create an empty table?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There are no properties. Yet again, see existing ObjectBuilder.finish()

final int idOffset = fbBuilder.writeString(id);

fbBuilder.startTable();
if (idOffset != null) {
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.

ints are nullable in dart, unless you're in null-safety mode in which case it'd be written as int?.

I don't think we should ever expect writeString to return null - for example if the buffer is out of space wouldn't we just throw?

Comment thread dart/test/monster_test_my_game.example_generated.dart
@dnfield
Copy link
Copy Markdown
Contributor

dnfield commented Jun 7, 2021

Needs tests :)

@vaind vaind force-pushed the dart-object-api branch from 4649459 to 63e255d Compare June 9, 2021 14:49
@vaind
Copy link
Copy Markdown
Contributor Author

vaind commented Jun 9, 2021

Added tests but they're blocked by an incorrect struct building I've found in the original code (i.e. unrelated to the changes in this branch). For that reason, part of the test case comparing the actually written and read data is commented out. After #6688 is resolved, this test case can be re-enabled and should pass.

@vaind vaind marked this pull request as ready for review June 9, 2021 14:52
@vaind vaind force-pushed the dart-object-api branch from f2c3da7 to 6537cca Compare June 11, 2021 07:48
@github-actions github-actions Bot added the documentation Documentation label Jun 11, 2021
@vaind
Copy link
Copy Markdown
Contributor Author

vaind commented Jun 12, 2021

Anything I should change for this to get merged? @dnfield @aardappel

Some discussions above can be resolved already?

@vaind vaind force-pushed the dart-object-api branch from 6537cca to cf41236 Compare June 12, 2021 10:30
Comment thread dart/test/flat_buffers_test.dart
Copy link
Copy Markdown
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

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

Lgtm

@vaind
Copy link
Copy Markdown
Contributor Author

vaind commented Jun 14, 2021

I've fixed the compilation on old MSVC with c++0x as reported by AppVeyor CI. Is there anything else to do here before the merge @aardappel?

@aardappel
Copy link
Copy Markdown
Collaborator

Nice, thanks!

@aardappel aardappel merged commit 4e3a66c into google:master Jun 14, 2021
@vaind vaind deleted the dart-object-api branch June 14, 2021 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema dart documentation Documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[dart] Object-based API

3 participants