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

WiP: Add DTO codegen #10

Merged
merged 4 commits into from Aug 9, 2022
Merged

WiP: Add DTO codegen #10

merged 4 commits into from Aug 9, 2022

Conversation

manosbatsis
Copy link
Contributor

@manosbatsis manosbatsis commented Jul 27, 2022

Nice work! Thought best to try contributing for my personal needs VS forking. This WiP PR adds support for generating DTOs, i.e. #9 nullable mutables that can be used for partial updates, e.g.

@FactoryFunctionImpl(Visibility.INTERNAL)
@ImmutableImpl(Visibility.INTERNAL)
@DtoImpl(Visibility.INTERNAL)
@MutableImpl(Visibility.INTERNAL)
@BuilderImpl(visibility = Visibility.INTERNAL)
@DSLBuilderImpl("sampleDSL", type = DSLBuilderImpl.Type.WITH_ACCESSORS, visibility = Visibility.INTERNAL)
interface Sample {
    val sample: String
    val number: Int
}

Will generate:

internal class DtoSample(
  public var sample: String?,
  public var number: Int?
) : Dto<Sample>

internal fun Sample.toDto(): DtoSample = DtoSample(sample, number)

internal fun Sample.toPatched(patch: Sample): Sample {
  val isMutable = this is MutableSample
  val base = if(isMutable) this as MutableSample else this.toMutable()
  patch.sample?.let{ 
    base.sample = it
  }
  patch.number?.let{ 
    base.number = it
  }
  return if(isMutable) this else base.toImmutable()
}

The toPatched shouldn't be so dependent on the generated MutableSample of course, see #11

@manosbatsis manosbatsis changed the title WiP: Add dto codegen WiP: Add DTO codegen Jul 27, 2022
@y9vad9 y9vad9 added the enhancement New feature or request label Aug 7, 2022
@y9vad9 y9vad9 self-requested a review August 7, 2022 15:17
@@ -4,7 +4,7 @@ object Deps {
const val compileSdkVersion = 31
const val minSdkVersion = 21

private const val kotlinVersion = "1.6.10"
private const val kotlinVersion = "1.6.0"
Copy link
Owner

@y9vad9 y9vad9 Aug 7, 2022

Choose a reason for hiding this comment

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

older version?

.addStatement("val base = if(isMutable) this as Mutable${declaration.simpleName.asString()} else this.toMutable()")
.also { builder ->
for (property in declaration.getAllProperties()) {
builder.addStatement("patch.${property.simpleName.asString()}?.let{ ")
Copy link
Owner

Choose a reason for hiding this comment

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

I think we should check type nullability

@y9vad9 y9vad9 merged commit 93cee34 into y9vad9:latest Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants