-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.scala
More file actions
923 lines (842 loc) · 35.3 KB
/
Copy pathcodegen.scala
File metadata and controls
923 lines (842 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
package gcp.codegen
import java.nio.file.*
import upickle.default.*
import GeneratorConfig.*
import scala.concurrent.{Future, Await}
import scala.concurrent.duration.*
import java.io.File
import scala.util.{Success, Try}
import scala.jdk.CollectionConverters.*
import scala.concurrent.ExecutionContext
extension (p: Path)
def /(o: Path) = Path.of(p.toString(), o.toString())
def /(o: Array[String]) = Path.of(p.toString(), o*)
def /(o: Vector[String]) = Path.of(p.toString(), o*)
def /(o: String) = Path.of(p.toString(), o)
case class GeneratorConfig(
outDir: Path,
outPkg: String,
httpSource: HttpSource,
jsonCodec: JsonCodec,
arrayType: ArrayType,
preprocess: Specs => Specs = s => s
)
object GeneratorConfig:
enum HttpSource:
case Sttp4
enum JsonCodec:
case ZioJson
case Jsoniter(jsonTypeRef: String)
enum ArrayType {
case List, Vector, Array, ZioChunk
def toScalaType(t: String): String = this match
case List => s"List[$t]"
case Vector => s"Vector[$t]"
case Array => s"Array[$t]"
case ZioChunk => s"zio.Chunk[$t]"
}
enum SpecsInput:
case StdIn
case FilePath(path: Path)
case class Task(
specsInput: SpecsInput,
config: GeneratorConfig
) {
def run(using ExecutionContext): Future[List[File]] = {
for {
content <- specsInput match
case SpecsInput.StdIn => Future(Console.in.lines().iterator().asScala.mkString)
case SpecsInput.FilePath(path) => Future(Files.readString(path))
specs = read[Specs](content)
files <- generateBySpec(
specs = config.preprocess(specs),
config = config
)
} yield files
}
def runAwait(timeout: Duration = 30.seconds, ec: ExecutionContext = ExecutionContext.global): List[File] =
given ExecutionContext = ec
Await.result(run, timeout)
}
def generateBySpec(
specs: Specs,
config: GeneratorConfig
)(using ExecutionContext): Future[List[File]] = {
val basePkgPath = config.outDir / config.outPkg.split('.')
val resourcesPkg = s"${config.outPkg}.resources"
val schemasPkg = s"${config.outPkg}.schemas"
val resourcesSplit = resourcesPkg.split('.')
val resourcesPath = config.outDir / resourcesSplit
val schemasPath = config.outDir / schemasPkg.split('.')
val commonCodecsObj = "codecs"
val commonCodecsPkg = s"$schemasPkg.$commonCodecsObj"
val commonCodecsPath = schemasPath / s"$commonCodecsObj.scala"
for {
_ <- Future {
(schemasPath +: resourcesPath +: specs.resources.keySet.map(_.dirPath(resourcesPath)).toSeq).foreach(
Files.createDirectories(_)
)
}
files <- Future
.sequence(
List(
Future {
val path = basePkgPath / s"${specs.name}.scala"
Files.writeString(
path,
List(
s"${toComment(List(s"${specs.title} ${specs.version}", specs.description, specs.documentationLink))}",
"",
s"package ${config.outPkg}",
"",
config.httpSource match {
case HttpSource.Sttp4 => "import sttp.model.*\nimport sttp.client4.*"
},
"",
s"""val baseUrl: Uri = uri"${specs.baseUrl}"""",
s"""val basePath: String = "${specs.basePath}"""",
s"""val rootUrl: Uri = uri"${specs.rootUrl}"""",
"",
s"opaque type QueryParameters = Map[String, String]",
"object QueryParameters:",
" extension (m: QueryParameters) def value: Map[String, String] = m",
" val empty: QueryParameters = Map.empty",
"",
" def apply(",
specs.queryParameters
.map((k, v) =>
s"""${{ toComment(v.description) }} ${toScalaName(k)}: ${v.typ
.withOptional(true)
.scalaType(config.arrayType, config.jsonCodec)} = None"""
)
.mkString(" ", ",\n ", ""),
"): QueryParameters =",
specs.queryParameters
.map((k, _) => s""" ${toScalaName(k)}.map(v => Map("$k" -> v.toString)).getOrElse(Map.empty)""")
.mkString(" ", "++ \n ", ""),
"",
if specs.endpoints.nonEmpty then "enum Endpoint(val location: String, val url: Uri):" else "",
specs.endpoints
.map(e =>
// for cases where location is not unique we will append numbers
val locationPostfix = specs.endpoints
.collect { case Endpoint(location = e.location, endpointUrl = endpointUrl) =>
endpointUrl
}
.zipWithIndex
.collectFirst { case (e.endpointUrl, idx) if idx > 0 => (idx + 1).toString() }
.getOrElse("")
s"""${toComment(
Some(e.description)
)} case `${e.location}$locationPostfix` extends Endpoint("${e.location}", uri"${e.endpointUrl}")""".stripMargin
)
.mkString("\n")
).mkString("\n")
)
List(path.toFile())
},
Future {
val path = resourcesPath / "resources.scala"
Files.writeString(
path,
List(
s"package $resourcesPkg",
"",
config.httpSource match {
case HttpSource.Sttp4 =>
"import sttp.model.*\nimport sttp.client4.*, sttp.client4.ResponseException.{DeserializationException, UnexpectedStatusCode}"
},
config.jsonCodec match {
case JsonCodec.ZioJson => "import zio.json.*"
case _: JsonCodec.Jsoniter => "import com.github.plokhotnyuk.jsoniter_scala.core.*"
},
s"val resourceRequest: PartialRequest[Either[String, String]] = basicRequest.headers(Header.contentType(MediaType.ApplicationJson))",
"",
s"export ${config.outPkg}.QueryParameters",
"",
(config.httpSource, config.jsonCodec) match
case (HttpSource.Sttp4, _: JsonCodec.Jsoniter) =>
"""|def asJson[T : JsonValueCodec]: ResponseAs[Either[ResponseException[String], T]] =
| asByteArrayAlways.mapWithMetadata((bytes, metadata) =>
| if metadata.isSuccess then
| try {
| Right(readFromArray[T](bytes))
| } catch {
| case e: JsonReaderException =>
| Left(DeserializationException(String(bytes, java.nio.charset.StandardCharsets.UTF_8), e, metadata))
| }
| else Left(UnexpectedStatusCode(String(bytes, java.nio.charset.StandardCharsets.UTF_8), metadata))
| )""".stripMargin
case (HttpSource.Sttp4, JsonCodec.ZioJson) =>
"""|def asJson[T : JsonDecoder]: ResponseAs[Either[ResponseException[String], T]] =
| asStringAlways.mapWithMetadata((body, metadata) =>
| if metadata.isSuccess then body.fromJson[T].left.map(e => DeserializationException(body, Exception(e), metadata))
| else Left(UnexpectedStatusCode(body, metadata))
| )""".stripMargin,
"",
config.httpSource match
case HttpSource.Sttp4 =>
"""|def asEmptyResponse: ResponseAs[Either[ResponseException[String], String]] =
| asStringAlways.mapWithMetadata((body, metadata) =>
| if metadata.isSuccess then Right(body)
| else Left(UnexpectedStatusCode(body, metadata))
| )""".stripMargin
).mkString("\n")
)
List(path.toFile())
},
Future
.traverse(specs.resources) { (resourceKey, resource) =>
val resourceName = resourceKey.scalaName
Future {
val code = resourceCode(
rootPkg = config.outPkg,
pkg = resourceKey.pkgName(resourcesPkg),
resourcesPkg = resourcesPkg,
schemasPkg = schemasPkg,
resourceName = resourceName,
resource = resource,
httpSource = config.httpSource,
hasProps = p => specs.hasProps(p),
arrType = config.arrayType,
commonQueryParams = specs.queryParameters,
jsonCodec = config.jsonCodec
)
val path = resourceKey.dirPath(resourcesPath) / s"$resourceName.scala"
Files.writeString(path, code)
path.toFile()
}
},
// generate schemas with properties
for {
schemas <- Future
.traverse(specs.schemas) { (schemaPath, schema) =>
Future {
val jsonType = config.jsonCodec match
case JsonCodec.ZioJson => "zio.json.ast.Json"
case JsonCodec.Jsoniter(jsonTypeRef) => jsonTypeRef
val code =
(if schema.properties.nonEmpty then
schemasCode(
schema = schema,
pkg = schemasPkg,
jsonCodec = config.jsonCodec,
hasProps = p => specs.hasProps(p),
arrType = config.arrayType
)
else
// create a type alias for objects without properties
val comment = toComment(schema.description)
s"""|package $schemasPkg
|
|${comment}type ${schema.id.scalaName} = Option[$jsonType]""".stripMargin
)
val path = schemasPath / s"${schemaPath.scalaName}.scala"
Files.writeString(path, code)
path.toFile()
}
}
} yield schemas.toList
)
)
.map(_.flatten)
} yield files
}
val scalaKeyWords = Set("type", "import", "val", "object", "enum", "export")
def toScalaTypeName(n: String): String = toScalaName(n.capitalize)
def toScalaName(n: String): String =
if scalaKeyWords.contains(n) then s"`$n`"
else n.replaceAll("[^a-zA-Z0-9_]", "")
def resourceCode(
rootPkg: String,
pkg: String,
resourcesPkg: String,
schemasPkg: String,
resourceName: String,
resource: Resource,
httpSource: HttpSource,
arrType: ArrayType,
hasProps: SchemaPath => Boolean,
commonQueryParams: Map[String, Parameter],
jsonCodec: JsonCodec
) =
val sttpClientPkg = httpSource match
case HttpSource.Sttp4 => "sttp.client4"
val sttpUriPkg = "sttp.model.Uri"
List(
s"package $pkg",
"",
s"import $schemasPkg.*",
s"import $resourcesPkg.*",
"",
s"object ${resourceName} {" +
resource.methods
.map { (k, method) =>
def pathSegments(urlPath: String) =
urlPath
.split("/")
.filter(_.nonEmpty)
.map(s =>
"\\{(.*?)\\}".r.findAllIn(s).toList match
case Nil => s"$sttpUriPkg.PathSegment(\"$s\")"
case v :: Nil if v == s =>
s"$sttpUriPkg.PathSegment(" + toScalaName(v.stripPrefix("{").stripSuffix("}")) + ")"
case vars =>
s"$sttpUriPkg.PathSegment(s\"" + vars
.foldLeft(s)((res, v) => res.replace(v, "$" + v.stripPrefix("{").stripSuffix("}"))) + "\")"
)
val req = method.mediaUploads match
case None => method.request.filter(_.schemaPath.forall(hasProps))
case Some(_) => None
val uploadProtocol = method.mediaUploads match
case Some(m) =>
Some {
val protocols = m.protocols.keySet.toList.sortBy {
case "simple" => 1
case _ => 10
}
// type and default
(protocols.mkString("\"", "\" | \"", "\""), protocols.head)
}
case None => None
val (requiredParams, optParams) = method.scalaParameters.partition(_._2.required)
def params(indent: String) =
requiredParams.map((n, t) =>
s"${toComment(t.description, indent)}$indent$n: ${t.scalaType(arrType, jsonCodec)}"
) :::
req.toList.map(r => s"${indent}request: ${r.scalaType(arrType, jsonCodec)}") :::
uploadProtocol.toList.map((typ, default) => s"${indent}uploadProtocol: $typ = \"$default\"") :::
optParams.map((n, t) =>
s"${toComment(t.description, indent)}$indent$n: ${t.scalaType(arrType, jsonCodec)} = None"
) :::
List(
s"${indent}endpointUrl: $sttpUriPkg = $rootPkg.baseUrl",
s"${indent}commonQueryParams: QueryParameters = " + ((
method.mediaUploads,
commonQueryParams.collectFirst { case ("uploadType", Parameter(_, _, e: SchemaType.Enum, _, _)) => e }
) match {
case (Some(m), Some(ut)) =>
s"""QueryParameters(uploadType = Some("${ut.values.head.value}"))"""
case _ => "QueryParameters.empty"
})
)
val setReqUri = method.mediaUploads match
case None =>
s"val requestUri = endpointUrl.addPathSegments(List(${pathSegments(method.urlPath).mkString(", ")}))"
case Some(m) =>
List(
"val requestUri = uploadProtocol match {",
m.protocols
.map((k, v) =>
s"""case "$k" => endpointUrl.withPathSegments(List(${pathSegments(v.path).mkString(", ")}))"""
)
.mkString(" ", "\n ", ""),
" }"
).mkString("\n")
val body = req match
case None => ""
case Some(_) => """.body(request.toJsonString)"""
val queryParams = "\n val params = " +
(method.scalaQueryParams match
case Nil => "commonQueryParams.value"
case qParams =>
qParams
.map {
case (k, p) if p.required => s"""Map("$k" -> $k)"""
case (k, p) => s"""$k.map(p => Map("$k" -> p.toString)).getOrElse(Map.empty)"""
}
.mkString("", " ++ ", " ++ commonQueryParams.value\n"))
def responseType(t: String) =
httpSource match
case HttpSource.Sttp4 =>
s"$sttpClientPkg.Request[Either[$sttpClientPkg.ResponseException[String], $t]]"
val (resType, mapResponse) = method.response match
case Some(r) if r.schemaPath.forall(hasProps) =>
val bodyType = r.scalaType(arrType, jsonCodec)
(
responseType(bodyType),
s".response(asJson[$bodyType])"
)
case _ => (responseType("String"), ".response(asEmptyResponse)")
s"""|${toComment(method.description)} def ${toScalaName(k)}(\n${params(indent = " ")
.mkString(",\n")}\n ): $resType = {$queryParams
| $setReqUri
| resourceRequest.${method.httpMethod.toLowerCase()}(requestUri.addParams(params))$body$mapResponse
| }""".stripMargin
}
.mkString("\n", "\n\n", "\n") +
"}"
).mkString("\n")
def schemasCode(
schema: Schema,
pkg: String,
jsonCodec: JsonCodec,
hasProps: SchemaPath => Boolean,
arrType: ArrayType
): String = {
def enums =
schema.properties.collect:
case (k, Property(_, SchemaType.Array(e: SchemaType.Enum, _), _)) => k -> e
case (k, Property(_, e: SchemaType.Enum, _)) => k -> e
def `def toJsonString`(objName: String) = jsonCodec match
case JsonCodec.ZioJson =>
s"def toJsonString: String = $objName.jsonCodec.encodeJson(this, None).toString()"
case _: JsonCodec.Jsoniter =>
s"def toJsonString: String = writeToString(this)"
def jsonDecoder(objName: String) =
List(
s"object $objName {",
jsonCodec match
case JsonCodec.ZioJson =>
s" given jsonCodec: JsonCodec[$objName] = JsonCodec.derived[$objName]"
case _: JsonCodec.Jsoniter =>
// Jsoniter doesn't support derivation from Scala 3 union types
enums
.map((k, e) =>
s" enum ${toScalaTypeName(k)} {\n${e.values.map(v => s"${toComment(Some(v.enumDescription), " ")} case ${toScalaName(v.value)}").mkString("\n ")}\n }\n"
)
.mkString("\n")
.appendedAll(
s"""| given jsonCodec: JsonValueCodec[$objName] =
| JsonCodecMaker.make(CodecMakerConfig.withAllowRecursiveTypes(true).withDiscriminatorFieldName(None))""".stripMargin
),
"}"
).mkString("\n")
def toSchemaClass(s: Schema): String =
val scalaName = s.id.scalaName
s"""|case class $scalaName(
|${s
.sortedProperties(hasProps)
.map { (n, t) =>
val enumType =
if jsonCodec == JsonCodec.ZioJson then SchemaType.EnumType.Literal
else SchemaType.EnumType.Nominal(s"$scalaName.${toScalaTypeName(n)}")
s"${toComment(t.withTypeDescription)} ${toScalaName(n)}: ${
(if (t.optional) s"${t.scalaType(arrType, jsonCodec, enumType)} = None"
else t.scalaType(arrType, jsonCodec, enumType))
}"
}
.mkString("", ",\n", "")}
|) {\n ${`def toJsonString`(scalaName)}\n}\n
|
|${jsonDecoder(scalaName)}
|""".stripMargin
List(
s"package $pkg",
"",
jsonCodec match {
case JsonCodec.ZioJson => "import zio.json.*"
case _: JsonCodec.Jsoniter =>
"""|import com.github.plokhotnyuk.jsoniter_scala.core.*
|import com.github.plokhotnyuk.jsoniter_scala.macros.*""".stripMargin
},
toSchemaClass(schema)
).mkString("\n")
}
case class FlatPath(path: String, params: List[String])
case class MediaUploadProtocol(multipart: Boolean, path: String) derives Reader
case class MediaUpload(protocols: Map[String, MediaUploadProtocol], accept: List[String]) derives Reader
case class Method(
httpMethod: String,
description: Option[String],
path: String,
flatPath: Option[FlatPath],
parameters: Map[String, Parameter],
parameterOrder: List[String],
response: Option[SchemaType],
request: Option[SchemaType] = None,
private val mediaUpload: Option[MediaUpload] = None
) {
private lazy val flatPathParams: List[(String, Parameter)] = flatPath.toList.flatMap(p =>
p.params.zipWithIndex.map((param, idx) =>
param -> Parameter(
// add descriptions of path prams to the first one
description = if idx == 0 then pathParamsDescription else None,
location = "path",
typ = SchemaType.Primitive("string", false, None),
required = true,
pattern = None
)
)
)
private def pathParamsDescription = parameters.toList
.collect { case _ -> Parameter(Some(d), "path", _, _, _) => d } match {
case Nil => None
case descs => Some(descs.mkString("\n"))
}
def urlPath: String = flatPath.map(_.path).getOrElse(path)
def mediaUploads: Option[MediaUpload] = mediaUpload.map(m =>
m.copy(protocols =
m.protocols.view
.mapValues(p =>
// map the path to flatPath on placeholders with pattern like {+var_name} if the same is found in method path
// need a better solution for this
"\\{(\\+.*?)\\}".r.findAllIn(p.path).toList match
case v :: Nil if path.contains(v) => p.copy(path = flatPath.map(_.path).getOrElse(p.path))
case _ => p
)
.toMap
)
)
// filter out path params if flatPath params are given
private lazy val pathParams: List[(String, Parameter)] =
parameters.toList.filterNot((_, p) => flatPathParams.nonEmpty && p.location == "path")
// non optional parameters first
def scalaParameters: List[(String, Parameter)] =
(flatPathParams ::: pathParams)
.map((k, v) => (toScalaName(k), v))
.sortBy(!_._2.required)
def scalaQueryParams: List[(String, Parameter)] = scalaParameters.filter(_._2.location == "query")
}
object Method:
given Reader[Method] = reader[ujson.Obj].map { o =>
Method(
httpMethod = o("httpMethod").str,
description = o.value.get("description").map(_.str),
path = o("path").str,
flatPath = o.value
.get("flatPath")
.map(_.str)
.map(path =>
FlatPath(
path = path,
params = "\\{(.*?)\\}".r.findAllIn(path).map(_.stripPrefix("{").stripSuffix("}")).toList
)
),
parameterOrder = o.value.get("parameterOrder").map(read[List[String]](_)).getOrElse(Nil),
parameters = o.value
.get("parameters")
.map(v => v.obj.map((k, v) => k -> Parameter.read(k, v)).toMap)
.getOrElse(Map.empty),
response = o.value
.get("response")
.map(r => SchemaType.readType(SchemaPath.empty, r.obj)),
request = o.value
.get("request")
.map(r => SchemaType.readType(SchemaPath.empty, r.obj)),
mediaUpload = o.value.get("supportsMediaUpload").map(f => f.bool) match
case Some(true) => o.value.get("mediaUpload").map(m => read[MediaUpload](m))
case _ => None
)
}
case class Resource(methods: Map[String, Method]) derives Reader
@scala.annotation.tailrec
def readResources(
resources: List[(ResourcePath, ujson.Value)],
result: Map[ResourcePath, Resource]
): Map[ResourcePath, Resource] =
resources match
case (k, v) :: xs =>
v.obj.remove("resources").map(_.obj) match
case None => readResources(xs, result.updated(k, read[Resource](v)))
case Some(obj) =>
val newRes = obj.map((a, b) => ResourcePath(k, a) -> b).toList ::: xs
Try(read[Resource](v)) match
case Success(res) => readResources(newRes, result.updated(k, res))
case _ => readResources(newRes, result)
case Nil => result
case class Parameter(
description: Option[String],
location: String,
typ: SchemaType,
required: Boolean = false,
pattern: Option[String] = None
) {
def scalaType(arrType: ArrayType, jsonCodec: JsonCodec): String =
typ.withOptional(!required).scalaType(arrType, jsonCodec)
}
object Parameter:
def read(name: String, o: ujson.Value) =
val typ = SchemaType.readType(SchemaPath(name), o.obj)
val typDesc = typ.description
Parameter(
description = o.obj.get("description").map(_.str).map(_ + typDesc.map("\n" + _).getOrElse("")),
location = o("location").str,
typ = typ,
required = o.obj.get("required").map(_.bool).getOrElse(false),
pattern = o.obj.get("pattern").map(_.str)
)
case class Property(description: Option[String], typ: SchemaType, readOnly: Boolean = false) {
def optional: Boolean = typ.optional || readOnly
def scalaType(arrType: ArrayType, jsonCodec: JsonCodec, enumType: SchemaType.EnumType): String =
typ.withOptional(optional).scalaType(arrType, jsonCodec, enumType)
def schemaPath: Option[SchemaPath] = typ.schemaPath
def nestedSchemaPath: Option[SchemaPath] = typ.schemaPath.filter(_.hasNested)
def withTypeDescription = typ match
case SchemaType.Array(SchemaType.Enum(_, values, _), _) =>
description.toList ::: values.map(e => s"${e.value}: ${e.enumDescription}")
case gcp.codegen.SchemaType.Enum(_, values, _) =>
description.toList ::: values.map(e => s"${e.value}: ${e.enumDescription}")
case _ => description.toList
}
object Property:
def readProperty(name: SchemaPath, o: ujson.Obj) =
Property(
description = o.value.get("description").map(_.str),
typ = SchemaType.readType(name, o),
readOnly = o.value.get("readOnly").map(_.bool).getOrElse(false)
)
enum SchemaType(val optional: Boolean):
case Ref(ref: SchemaPath, override val optional: Boolean) extends SchemaType(optional)
case Primitive(
`type`: "string" | "integer" | "number" | "boolean",
override val optional: Boolean,
format: Option[String] = None
) extends SchemaType(optional)
case Any(override val optional: Boolean) extends SchemaType(optional)
case Array(items: SchemaType, override val optional: Boolean) extends SchemaType(optional)
case Object(additionalProperties: SchemaType, override val optional: Boolean) extends SchemaType(optional)
case Enum(typ: String, values: List[SchemaType.EnumValue], override val optional: Boolean) extends SchemaType(true)
private def toType(t: String) = if optional then s"Option[$t]" else t
def description: Option[String] = this match
case Enum(_, values, _) =>
values.collect { case SchemaType.EnumValue(n, desc) if desc.nonEmpty => s"$n: $desc" } match
case Nil => None
case v => Some(v.mkString("\n"))
case _ => None
def schemaPath: Option[SchemaPath] = this match
case Ref(ref, _) => Some(ref)
case Array(Ref(ref, _), _) => Some(ref)
case _ => None
def withOptional(o: Boolean) = this match
case t: Ref => t.copy(optional = o)
case t: Primitive => t.copy(optional = o)
case t: Array => t.copy(optional = o)
case t: Object => t.copy(optional = o)
case t: Enum => t.copy(optional = o)
case t: Any => t.copy(optional = o)
def scalaType(
arrayType: ArrayType,
jsonCodec: JsonCodec,
enumType: SchemaType.EnumType = SchemaType.EnumType.Literal
): String = this match
case Primitive("string", _, Some("google-datetime")) => toType("java.time.OffsetDateTime")
case Primitive("string", _, _) => toType("String")
case Primitive("integer", _, Some("int32" | "uint32")) => toType("Int")
case Primitive("integer", _, Some("int64" | "uint64")) => toType("Long")
case Primitive("number", _, Some("double" | "float")) => toType("Double")
case Primitive("boolean", _, _) => toType("Boolean")
case Ref(ref, _) => toType(ref.scalaName)
case Array(t, _) => toType(arrayType.toScalaType(t.scalaType(arrayType, jsonCodec, enumType)))
case Object(t: Primitive, _) => toType(s"Map[String, ${t.scalaType(arrayType, jsonCodec)}]")
case _: Object =>
toType(
jsonCodec match
case JsonCodec.ZioJson => "zio.json.ast.Json.Obj"
case JsonCodec.Jsoniter(jsonRef) => jsonRef
)
case Enum(_, values, _) =>
enumType match
case SchemaType.EnumType.Literal => toType(values.map(v => v.value).mkString("\"", "\" | \"", "\""))
case SchemaType.EnumType.Nominal(name) => toType(name)
case _ =>
toType(
jsonCodec match
case JsonCodec.ZioJson => "zio.json.ast.Json"
case JsonCodec.Jsoniter(jsonRef) => jsonRef
)
object SchemaType:
case class EnumValue(value: String, enumDescription: String)
enum EnumType:
case Literal
case Nominal(prefix: String)
private def toPrimitiveOrAny(o: ujson.Obj, optional: Boolean) =
o("type").str match
case typ: ("string" | "integer" | "number" | "boolean") =>
SchemaType.Primitive(
`type` = typ,
optional = optional,
format = o.value.get("format").map(_.str)
)
case _ => Any(optional)
def readType(context: SchemaPath, o: ujson.Obj): SchemaType =
val desc = o.value.get("description").map(_.str)
val optional = desc
.map(_.toLowerCase())
.exists(d => d.startsWith("optional") || !d.startsWith("required"))
o.value.get("items").map(_.obj) match
case Some(v) =>
SchemaType.Array(readType(context.add("items"), v), optional)
case _ =>
(o.value.get("enum")) match
case Some(e) =>
SchemaType.Enum(
typ = o("type").str,
read[List[String]](e)
.zip(read[List[String]](o("enumDescriptions")))
.map((v, vd) => EnumValue(value = v, enumDescription = vd)),
optional
)
case _ =>
o.value.get("additionalProperties").map(_.obj) match
case Some(v) =>
SchemaType.Object(
readType(context.add("additionalProperties"), v),
optional
)
case _ =>
o.value.get("$ref").map(_.str) match
case Some(ref) => SchemaType.Ref(SchemaPath(ref), optional)
case _ =>
if !o.value.keySet.contains("properties") then
if Set("uploadType", "upload_protocol").exists(context.jsonPath.lastOption.contains) then
""""([\w]+)"""".r
.findAllMatchIn(desc.getOrElse(""))
.map(_.group(1))
.toList
.collect { case v: String => EnumValue(value = v, enumDescription = "") } match
case Nil => toPrimitiveOrAny(o, optional)
case values => SchemaType.Enum(typ = o("type").str, optional = optional, values = values)
else toPrimitiveOrAny(o, optional)
else SchemaType.Ref(context, optional)
opaque type SchemaPath = Vector[String]
object SchemaPath:
val empty: SchemaPath = Vector.empty
def apply(name: String): SchemaPath = Vector(name)
extension (s: SchemaPath)
def scalaName: String =
s.filter(!Set("items", "properties").contains(_)).map(toScalaTypeName(_)).mkString
def add(nested: String): SchemaPath = s.appended(nested)
def hasNested: Boolean = s.size > 1
def jsonPath: Vector[String] = s
case class Schema(
id: SchemaPath,
description: Option[String],
properties: List[(String, Property)]
) {
def hasRequired: Boolean = properties.exists(!_._2.typ.optional)
def hasArrays: Boolean = properties.exists(_._2.typ match {
case gcp.codegen.SchemaType.Array(_, _) => true
case _ => false
})
// required properties first
// references wihout properties are excluded
def sortedProperties(hasProps: SchemaPath => Boolean): List[(String, Property)] =
properties
.filter { (_, prop) =>
prop.schemaPath.forall(hasProps(_))
}
.sortBy(_._2.typ.optional)
}
object Schema:
private def readProps(
props: ujson.Obj,
parentName: SchemaPath
): List[(String, Property)] =
props.value.view
.mapValues(_.obj)
.map { (k, obj) =>
val name = parentName.add(k)
(k, Property.readProperty(name, obj))
}
.toList
private def readSchema(name: SchemaPath, data: ujson.Obj): Schema =
Schema(
id = name,
description = data.value.get("description").map(_.str),
properties = data.value.get("properties").map(_.obj).map(readProps(_, name.add("properties"))).getOrElse(Nil)
)
def readSchemas(o: ujson.Obj): Map[SchemaPath, Schema] = {
@scala.annotation.tailrec
def readSchemas(
schemas: List[(SchemaPath, ujson.Obj)],
result: Map[SchemaPath, Schema]
): Map[SchemaPath, Schema] =
schemas match {
case Nil => result
case (name, data) :: xs =>
val schema = readSchema(name, data)
schema.properties
.map(_._2.nestedSchemaPath)
.flatMap {
case None => Nil
case Some(p) => List((p, p.jsonPath.foldLeft(o)(_.apply(_).obj)))
} match {
case Nil => readSchemas(xs, result.updated(name, schema))
case nested =>
readSchemas(nested ::: xs, result.updated(name, schema))
}
}
readSchemas(
o.value.map((k, json) => (SchemaPath(k), ujson.Obj(json.obj))).toList,
Map.empty
)
}
opaque type ResourcePath = Vector[String]
object ResourcePath:
def apply(p: String): ResourcePath = Vector(p)
def apply(pp: Vector[String], p: String): ResourcePath = pp :+ p
extension (r: ResourcePath)
def add(p: String): ResourcePath = r :+ p
def scalaName: String = toScalaTypeName(r.last)
def pkgPath: Vector[String] = r.dropRight(1).map(camelToSnakeCase)
def pkgName(base: String): String = s"$base${if pkgPath.nonEmpty then pkgPath.mkString(".", ".", "") else ""}"
def dirPath(base: Path): Path = base / pkgPath
def matches(v: String): Boolean =
val regex = s"^${v.replace(".", "\\.").replace("*", ".*")}$$"
val path = r.mkString(".")
path.matches(regex)
def hasMatch(v: Seq[String]): Boolean = v.exists(matches)
case class Endpoint(endpointUrl: String, location: String, description: String) derives Reader
case class Specs(
id: String,
name: String,
title: String,
description: String,
revision: String,
documentationLink: String,
protocol: String,
ownerName: String,
discoveryVersion: String,
resources: Map[ResourcePath, Resource],
version: String,
schemas: Map[SchemaPath, Schema],
rootUrl: String,
baseUrl: String,
basePath: String,
endpoints: List[Endpoint],
queryParameters: Map[String, Parameter]
) {
def hasProps(schemaName: SchemaPath): Boolean =
schemas.get(schemaName).exists(_.properties.nonEmpty)
}
def camelToSnakeCase(camelCase: String): String = {
val camelCaseRegex = "([A-Z][a-z]+)".r
camelCaseRegex.replaceAllIn(camelCase, matched => "_" + matched.group(0).toLowerCase)
}
// comment splitted into multipl lines
private def toComment(content: Iterable[String], indent: String = " "): String =
if content.isEmpty then ""
else
content
.flatMap(_.split('\n'))
.flatMap(_.replace("e.g. ", "e.g.: ").split("\\. "))
.filter(_.trim.nonEmpty)
.mkString(s"$indent// ", s"\n$indent// ", "\n")
object Specs:
given Reader[Specs] = reader[ujson.Obj].map(o =>
Specs(
id = o("id").str,
title = o("title").str,
description = o("description").str,
revision = o("revision").str,
name = o("name").str,
documentationLink = o("documentationLink").str,
protocol = o("protocol").str,
ownerName = o("ownerName").str,
discoveryVersion = o("discoveryVersion").str,
resources = readResources(
o("resources").obj.map((k, v) => ResourcePath(k) -> v).toList,
Map.empty
),
version = o("version").str,
baseUrl = o("baseUrl").str,
rootUrl = o("rootUrl").str,
schemas = Schema.readSchemas(o("schemas").obj),
basePath = o("basePath").str,
endpoints = o.value.get("endpoints").map(read[List[Endpoint]](_)).getOrElse(Nil),
queryParameters = o.value.get("parameters") match
case None => Map.empty
case Some(v) => v.obj.map((k, v) => k -> Parameter.read(k, v)).filter(_._2.location == "query").toMap
)
)