Skip to content
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

Fix Gen Class Without Any Properties #15

Merged
merged 4 commits into from
Oct 16, 2023
Merged

Conversation

tamnguyenhuy
Copy link
Contributor

@tamnguyenhuy tamnguyenhuy commented Oct 13, 2023

Change fix generate class without Any Properties

syntax = "proto3";

message Hello {
  enum Type {
    UNKNOWN = 0;
    KNOWN = 1;
  }
  message SubHello {
    message HelloExtend {
      optional Type type = 1;
    }
    optional SubHello subHello = 1;
  }
}

Result

package testgen

import kotlinx.serialization.Serializable
import kotlinx.serialization.protobuf.ProtoNumber

@Serializable
public class Hello {
  @Serializable
  public data class SubHello(
    @ProtoNumber(number = 1)
    public var subHello: SubHello? = null,
  ) {
    @Serializable
    public data class HelloExtend(
      @ProtoNumber(number = 1)
      public var type: Type? = null,
    )
  }

  @Serializable
  public enum class Type {
    @ProtoNumber(number = 0)
    UNKNOWN,
    @ProtoNumber(number = 1)
    KNOWN,
  }
}

Fixes #20

@tamnguyenhuy
Copy link
Contributor Author

@Dogacel please help to take a look at my PR and let me know your thought!!
Thanks

Copy link
Owner

@Dogacel Dogacel left a comment

Choose a reason for hiding this comment

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

Happy to have you as the first contributor 🎉

There are some minor fixes we gotta do. I am not sure if the CI/CD pipeline is ready for PRs as well. I will check it tomorrow morning.

Please let me know if you need any help, I can jump in to fix those small nits 😄

@@ -0,0 +1,14 @@
syntax = "proto3";
Copy link
Owner

Choose a reason for hiding this comment

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

Can you run ./gradlew build in your local? I commit all the generated classes under generated-code-tests. Also we should have a test there to verify an empty message without any properties are serialized correctly.

@@ -193,7 +212,7 @@ class CodeGenerator {
* @param messageDescriptor [Descriptors.Descriptor] to generate code for.
* @return [TypeSpec.Builder] that contains the generated code.
*/
private fun generateSingleClass(messageDescriptor: Descriptors.Descriptor): TypeSpec.Builder {
private fun generateDataClass(messageDescriptor: Descriptors.Descriptor): TypeSpec.Builder {
val typeSpec = TypeSpec.classBuilder(messageDescriptor.name)
.addModifiers(KModifier.DATA)
Copy link
Owner

Choose a reason for hiding this comment

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

One thing I would suggest to simplify the code and reduce duplication is to add this modifier conditionally based on isHasPropertyField call. Simply we can hide this logic behind the generateSingleClass method so users don't have to worry about figuring out which method to call 🙂

*/
private fun isHasPropertyField(fileDescriptor: Descriptors.Descriptor): Boolean {
return fileDescriptor.fields.any {
it.isOptional || it.isRequired || it.isRepeated
Copy link
Owner

Choose a reason for hiding this comment

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

I have a question, in proto3 if optional int32 x = 1 is considered optional and in proto2 if required int32 x = 1 is considered required, is this going to return false int32 x = 1?

I think we should have a couple of unit tests to verify this doesn't break anything.

@codecov-commenter
Copy link

Welcome to Codecov 🎉

Once merged to your default branch, Codecov will compare your coverage reports and display the results in this comment.

Thanks for integrating Codecov - We've got you covered ☂️

@Dogacel
Copy link
Owner

Dogacel commented Oct 13, 2023

@tamnguyenhuy

I have added some additional checks to make sure we generate the code. Also updated codecov to show coverage info.

I had to merge latest main into your PR to see those. Don't forget to fetch remote 😉

@Dogacel Dogacel merged commit 668d2cd into Dogacel:main Oct 16, 2023
2 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Message without any properties is not compilable
3 participants