Skip to content

Support Thrift TypeDef in DocService #4628

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 1 commit into from
Feb 7, 2023
Merged

Conversation

minwoox
Copy link
Contributor

@minwoox minwoox commented Jan 12, 2023

Motivation:
Currently, we don't resolve the Thrift typedef fields correctly.
image

Modifications:

  • Resolve Thrift typedef fields correctly using Field.getGenericType().
  • Use i8 instead of byte for Thrift 0.12+ to remove the warning.

Result:

  • The Thrift typedefs are correctly resolved.

image

Verified

This commit was signed with the committer’s verified signature.
minwoox minux
Motivation:

https://thrift.apache.org/docs/idl#typedef
Modifications:

Result:
Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

Nice! 🙇‍♂️

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

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

Looks good! Left a minor question 🙏

Comment on lines +174 to +184
if (!fieldValueMetaData.isTypedef() && !hasTypeDef(fieldValueMetaData)) {
return toTypeSignature(fieldValueMetaData);
}

try {
final Field field = parentType.getField(fieldMetaData.fieldName);
return toTypeSignature(field.getGenericType());
} catch (NoSuchFieldException e) {
// Ignore exception.
}
return TypeSignature.ofUnresolved(firstNonNull(fieldValueMetaData.getTypedefName(), "unknown"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Question) Is it possible to just use java reflection and clean up the old logic?
I ask because if a typedef is somewhere nested inside the struct, we will use generics instead of thrift objects to traverse the object tree anyways

Suggested change
if (!fieldValueMetaData.isTypedef() && !hasTypeDef(fieldValueMetaData)) {
return toTypeSignature(fieldValueMetaData);
}
try {
final Field field = parentType.getField(fieldMetaData.fieldName);
return toTypeSignature(field.getGenericType());
} catch (NoSuchFieldException e) {
// Ignore exception.
}
return TypeSignature.ofUnresolved(firstNonNull(fieldValueMetaData.getTypedefName(), "unknown"));
try {
final Field field = parentType.getField(fieldMetaData.fieldName);
return toTypeSignature(field.getGenericType());
} catch (NoSuchFieldException e) {
// Ignore exception.
}
return TypeSignature.ofUnresolved(firstNonNull(fieldValueMetaData.getTypedefName(), "unknown"));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if a typedef is somewhere nested inside the struct, we will use generics instead of thrift objects to traverse the object tree anyways

Yes, exactly. That's why I added hasTypeDef method.

I left the old logic because there are not so many people who use type def with Armeria(type def doesn't do anything with Java).
In that case, I thought using explicit metadata instead of using implicit reflection is better.
But not so strong on this because we can integrate the logic into one if we just use reflection.
So please let me know if you think it's better to use just reflection for all cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

My original concern was that if we have to troubleshoot anything, we would have to guess which code path was used.
Having said this, I'm also fine to go ahead with the fix and clean up later if necessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

type def doesn't do anything with Java

I think I've seen some usage with typedef to abstract logic within the thrift idl (especially if the idl becomes very long and complex)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got your concern. If so, what if I extract them as a method and add documentation for that? It will make it clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I've seen some usage with typedef to abstract logic within the thrift idl

Oops, I didn't know about it. 😓
So are they using DocService with the type definition?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for merging the PR in the middle of the discussion.
Personally, I prefer to use expose the original type because a type alias may be used to share a type in multiple structures but DocService shows one structure at a time.

We may improve DocService to display both type information and alias properly somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We may improve DocService to display both type information and alias properly somehow.

Indeed. 😊

final Field field = parentType.getField(fieldMetaData.fieldName);
return toTypeSignature(field.getGenericType());
} catch (NoSuchFieldException e) {
// Ignore exception.
Copy link
Contributor

Choose a reason for hiding this comment

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

Question) Is there a particular exception that we wanted to ignore? If not, I wonder if debugging would help with troubleshooting potential issues

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought NoSuchFieldException won't be raised and just added the catch clause because it's a checked exception.
So you mean we need to at least leave a debug log?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I see, I guess there's no need to leave a log if we're sure this won't happen 😅

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

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

Thanks for the fix @minwoox ! 🙇 👍 🙇

@ikhoon ikhoon merged commit 36164e2 into line:master Feb 7, 2023
@minwoox minwoox deleted the thriftTypeDef branch March 29, 2023 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants