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

Allow for definition of classname that does not require import #1435

Open
brizzbuzz opened this issue Jan 3, 2023 · 0 comments
Open

Allow for definition of classname that does not require import #1435

brizzbuzz opened this issue Jan 3, 2023 · 0 comments

Comments

@brizzbuzz
Copy link

Is your feature request related to a problem? Please describe.

Apologies if there is already a way to do this, but I have not had any luck coming up with a particularly intuitive way of doing this.

Let's say I am trying to achieve a nested structure for a number of data classes

data class User(val name: String, val metadata: Metadata) {
  companion object {
    fun new(): User { /**/ }
  } 

  data class Metadata(val age: Int, val weight) {
    companion object {
      // Main problem 👇
      fun new(): Metadata { /**/ }
    }
  }
}

Most of this is quite easily achievable in Kotlinpoet. However, what I am struggling with is a concise way to properly assign the return type of Metadata.new. When writing this manually, it is perfect valid to simply specify Metadata as the return type, no need for additional imports. However, there does not seem to be an easy way to express this in Kotlinpoet.

My current approach is to resort to a recursion tracker so that I can keep track of $BASE_PACKAGE.User.Metadata (in my actual code this can be of arbitrary depth).

Describe the solution you'd like
A way to allow users to define ClassNames that are guaranteed to already be imported. Alternatively, a way to access the ClassName from within a TypeSpec.Builder however, I am not sure that is feasible, would totally make sense if that information is not available until the actual TypeSpec is constructed

Describe alternatives you've considered
Oh... see above

Additional context
N/A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant