@@ -9,7 +9,7 @@ import com.avsystem.justworks.core.gen.GENERATED_SERIALIZERS_MODULE
99import com.avsystem.justworks.core.gen.HTTP_CLIENT
1010import com.avsystem.justworks.core.gen.HTTP_ERROR
1111import com.avsystem.justworks.core.gen.HTTP_SUCCESS
12- import com.avsystem.justworks.core.gen.ModelPackage
12+ import com.avsystem.justworks.core.gen.Hierarchy
1313import com.avsystem.justworks.core.gen.NameRegistry
1414import com.avsystem.justworks.core.gen.RAISE
1515import com.avsystem.justworks.core.gen.TOKEN
@@ -51,29 +51,22 @@ internal object ClientGenerator {
5151 private const val DEFAULT_TAG = " Default"
5252 private const val API_SUFFIX = " Api"
5353
54- context(_: ModelPackage , _: ApiPackage )
55- fun generate (
56- spec : ApiSpec ,
57- hasPolymorphicTypes : Boolean ,
58- nameRegistry : NameRegistry ,
59- ): List <FileSpec > {
54+ context(_: Hierarchy , _: ApiPackage , _: NameRegistry )
55+ fun generate (spec : ApiSpec , hasPolymorphicTypes : Boolean ): List <FileSpec > {
6056 val grouped = spec.endpoints.groupBy { it.tags.firstOrNull() ? : DEFAULT_TAG }
61- return grouped.map { (tag, endpoints) ->
62- generateClientFile(tag, endpoints, hasPolymorphicTypes, nameRegistry)
63- }
57+ return grouped.map { (tag, endpoints) -> generateClientFile(tag, endpoints, hasPolymorphicTypes) }
6458 }
6559
66- context(modelPackage : ModelPackage , apiPackage: ApiPackage )
60+ context(hierarchy : Hierarchy , apiPackage: ApiPackage , nameRegistry : NameRegistry )
6761 private fun generateClientFile (
6862 tag : String ,
6963 endpoints : List <Endpoint >,
7064 hasPolymorphicTypes : Boolean ,
71- nameRegistry : NameRegistry ,
7265 ): FileSpec {
7366 val className = ClassName (apiPackage, nameRegistry.register(" ${tag.toPascalCase()}$API_SUFFIX " ))
7467
7568 val clientInitializer = if (hasPolymorphicTypes) {
76- val generatedSerializersModule = MemberName (modelPackage, GENERATED_SERIALIZERS_MODULE )
69+ val generatedSerializersModule = MemberName (hierarchy. modelPackage, GENERATED_SERIALIZERS_MODULE )
7770 CodeBlock .of(" ${CREATE_HTTP_CLIENT } (%M)" , generatedSerializersModule)
7871 } else {
7972 CodeBlock .of(" ${CREATE_HTTP_CLIENT } ()" )
@@ -101,17 +94,18 @@ internal object ClientGenerator {
10194 .primaryConstructor(primaryConstructor)
10295 .addProperty(httpClientProperty)
10396
104- val methodRegistry = NameRegistry ()
105- classBuilder.addFunctions(endpoints.map { generateEndpointFunction(it, methodRegistry) })
97+ context(NameRegistry ()) {
98+ classBuilder.addFunctions(endpoints.map { generateEndpointFunction(it) })
99+ }
106100
107101 return FileSpec
108102 .builder(className)
109103 .addType(classBuilder.build())
110104 .build()
111105 }
112106
113- context(_: ModelPackage )
114- private fun generateEndpointFunction (endpoint : Endpoint , methodRegistry : NameRegistry ): FunSpec {
107+ context(_: Hierarchy , methodRegistry : NameRegistry )
108+ private fun generateEndpointFunction (endpoint : Endpoint ): FunSpec {
115109 val functionName = methodRegistry.register(endpoint.operationId.toCamelCase())
116110 val returnBodyType = resolveReturnType(endpoint)
117111 val returnType = HTTP_SUCCESS .parameterizedBy(returnBodyType)
@@ -166,7 +160,7 @@ internal object ClientGenerator {
166160 return funBuilder.build()
167161 }
168162
169- context(_: ModelPackage )
163+ context(_: Hierarchy )
170164 private fun resolveReturnType (endpoint : Endpoint ): TypeName = endpoint.responses.entries
171165 .asSequence()
172166 .filter { it.key.startsWith(" 2" ) }
0 commit comments