-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kpoliwka/create case class from yaml (#534)
* created crd module in codegen using CR overlays for kubernetes @polkx * moved crd module to separate crd2besom module @lbialy --------- Co-authored-by: Łukasz Biały <[email protected]>
- Loading branch information
Showing
18 changed files
with
1,588 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ src/main/scala/besom/rpc | |
.out/ | ||
|
||
*.asc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version = 3.5.2 | ||
runner.dialect = scala3 | ||
project.git = true | ||
align = most | ||
align.openParenCallSite = false | ||
align.openParenDefnSite = false | ||
align.tokens = [{code = "=>", owner = "Case"}, "<-", "%", "%%", "="] | ||
indent.defnSite = 2 | ||
maxColumn = 140 | ||
|
||
rewrite.scala3.insertEndMarkerMinLines = 40 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//> using scala 3.3.1 | ||
//> using options -java-output-version:11 | ||
//> using options -deprecation -feature -Werror -Wunused:all | ||
|
||
//> using dep org.virtuslab::besom-codegen:0.4.0-SNAPSHOT | ||
//> using dep org.virtuslab::scala-yaml:0.1.0 | ||
|
||
//> using dep org.scalameta::munit:1.0.1 | ||
|
||
//> using publish.name "besom-crd2besom" | ||
//> using publish.organization "org.virtuslab" | ||
//> using publish.url "https://github.com/VirtusLab/besom" | ||
//> using publish.vcs "github:VirtusLab/besom" | ||
//> using publish.license "Apache-2.0" | ||
//> using publish.repository "central" | ||
//> using publish.developer "lbialy|Łukasz Biały|https://github.com/lbialy" | ||
//> using publish.developer "pawelprazak|Paweł Prażak|https://github.com/pawelprazak" | ||
//> using repository sonatype:snapshots |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package besom.codegen.crd | ||
|
||
import scala.meta.* | ||
import scala.meta.dialects.Scala33 | ||
import besom.codegen.scalameta.interpolator.* | ||
import besom.codegen.scalameta.ref | ||
|
||
enum AdditionalCodecs(val name: Type, val codecs: Seq[Stat]): | ||
case LocalDateTime | ||
extends AdditionalCodecs( | ||
Type.Select(ref("java", "time"), Type.Name("LocalDateTime")), | ||
Seq( | ||
m""" | ||
| given besom.Encoder[java.time.LocalDateTime] with | ||
| def encode(t: java.time.LocalDateTime)(using besom.Context): besom.internal.Result[(besom.internal.Metadata, com.google.protobuf.struct.Value)] = | ||
| besom.internal.Encoder.stringEncoder.encode(t.format(java.time.format.DateTimeFormatter.ISO_DATE_TIME)) | ||
|""", | ||
m""" | ||
| given besom.Decoder[java.time.LocalDateTime] with | ||
| def mapping(v: com.google.protobuf.struct.Value, label: besom.types.Label): besom.util.Validated[besom.internal.DecodingError, java.time.LocalDateTime] = | ||
| besom.internal.Decoder.stringDecoder.mapping(v, label).flatMap(str => | ||
| scala.util.Try(java.time.LocalDateTime.parse(v.getStringValue, java.time.format.DateTimeFormatter.ISO_DATE_TIME)) match | ||
| case scala.util.Success(value) => | ||
| besom.util.Validated.valid(value) | ||
| case scala.util.Failure(_) => | ||
| besom.util.Validated.invalid(besom.internal.Decoder.error(s"$$label: Expected a LocalDateTime, got: '$${v.kind}'", label)) | ||
| ) | ||
|""" | ||
).map(_.stripMargin.parse[Stat].get) | ||
) | ||
|
||
case LocalDate | ||
extends AdditionalCodecs( | ||
Type.Select(ref("java", "time"), Type.Name("LocalDate")), | ||
Seq( | ||
m""" | ||
| given besom.Encoder[java.time.LocalDate] with | ||
| def encode(t: java.time.LocalDate)(using besom.Context): besom.internal.Result[(besom.internal.Metadata, com.google.protobuf.struct.Value)] = | ||
| besom.internal.Encoder.stringEncoder.encode(t.format(java.time.format.DateTimeFormatter.ISO_DATE)) | ||
|""", | ||
m""" | ||
| given besom.Decoder[java.time.LocalDate] with | ||
| def mapping(v: com.google.protobuf.struct.Value, label: besom.types.Label): besom.util.Validated[besom.internal.DecodingError, java.time.LocalDate] = | ||
| besom.internal.Decoder.stringDecoder.mapping(v, label).flatMap(str => | ||
| scala.util.Try(java.time.LocalDate.parse(v.getStringValue, java.time.format.DateTimeFormatter.ISO_DATE)) match | ||
| case scala.util.Success(value) => | ||
| besom.util.Validated.valid(value) | ||
| case scala.util.Failure(_) => | ||
| besom.util.Validated.invalid(besom.internal.Decoder.error(s"$$label: Expected a LocalDate, got: '$${v.kind}'", label)) | ||
| ) | ||
|""" | ||
).map(_.stripMargin.parse[Stat].get) | ||
) | ||
end AdditionalCodecs | ||
|
||
object AdditionalCodecs: | ||
private val nameToValuesMap: Map[String, AdditionalCodecs] = | ||
AdditionalCodecs.values.map(c => c.name.syntax -> c).toMap | ||
|
||
def getCodec(name: Type): Option[AdditionalCodecs] = | ||
nameToValuesMap.get(name.syntax) | ||
|
||
private def enumEncoder(enumName: String): Stat = | ||
m""" | ||
| given besom.Encoder[$enumName] with | ||
| def encode(e: $enumName)(using besom.Context): besom.internal.Result[(besom.internal.Metadata, com.google.protobuf.struct.Value)] = | ||
| besom.internal.Encoder.stringEncoder.encode(e.toString) | ||
|""".stripMargin.parse[Stat].get | ||
|
||
private def enumDecoder(enumName: String): Stat = | ||
m""" | ||
| given besom.Decoder[$enumName] with | ||
| def mapping(v: com.google.protobuf.struct.Value, label: besom.types.Label): besom.util.Validated[besom.internal.DecodingError, $enumName] = | ||
| besom.internal.Decoder.stringDecoder.mapping(v, label).flatMap(str => | ||
| scala.util.Try($enumName.valueOf(str)) match | ||
| case scala.util.Success(value) => | ||
| besom.util.Validated.valid(value) | ||
| case scala.util.Failure(_) => | ||
| besom.util.Validated.invalid(besom.internal.Decoder.error(s"$$label: Expected a $enumName enum, got: '$${v.kind}'", label)) | ||
| ) | ||
|""".stripMargin.parse[Stat].get | ||
|
||
def enumCodecs(enumName: String): Seq[Stat] = Seq(enumEncoder(enumName), enumDecoder(enumName)) | ||
end AdditionalCodecs |
Oops, something went wrong.