Skip to content

Commit

Permalink
Kubernetes 1.11 and Semiauto encoding (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszym authored and joan38 committed Aug 14, 2018
1 parent 9f712d5 commit 5ad9ac3
Show file tree
Hide file tree
Showing 6 changed files with 88,359 additions and 73,694 deletions.
50 changes: 41 additions & 9 deletions project/SwaggerModelGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,36 @@ object SwaggerModelGenerator extends AutoPlugin {
swaggerFiles.flatMap(processSwaggerFile(_, (sourceManaged in Compile).value, streams.value.log))
}

def classNameFilter(className: String): Boolean = {
val allowedPrefixes = Seq(
"io.k8s.api.apps.v1",
"io.k8s.api.core.v1",
"io.k8s.api.rbac.v1",
"io.k8s.api.batch.v1",
"io.k8s.kubernetes.pkg.apis.policy.v1beta1",
"io.k8s.api.policy.v1beta1",
"io.k8s.apimachinery.pkg.runtime",
"io.k8s.api.storage.v1",
"io.k8s.api.autoscaling.v1",
"io.k8s.apimachinery.pkg.api",
"io.k8s.kubernetes.pkg.apis.storage.v1",
"io.k8s.apimachinery.pkg.apis.meta.v1",
"io.k8s.kubernetes.pkg.api.v1",
"io.k8s.kubernetes.pkg.apis.batch.v1",
"io.k8s.kubernetes.pkg.apis.networking.v1"
)
allowedPrefixes.exists(className.startsWith)
}

def processSwaggerFile(swaggerFile: File, outputDir: File, log: Logger) = {
val json = parse(IO.read(swaggerFile)).fold(throw _, identity)
for {
definitionsJson <- json.hcursor.downField("definitions").focus.toSeq
definitionsObject <- definitionsJson.asObject.toSeq
classFile <- definitionsObject.toMap.map {
case (fullClassName, definition) => generateDefinition(fullClassName, definition, outputDir, log)
}
} yield classFile

(fullClassName, definition) <- definitionsObject.toMap
if classNameFilter(fullClassName)
} yield generateDefinition(fullClassName, definition, outputDir, log)
}

def generateDefinition(fullClassName: String, definitionJson: Json, outputDir: File, log: Logger) = {
Expand All @@ -45,21 +66,32 @@ object SwaggerModelGenerator extends AutoPlugin {
case Definition(desc, required, properties, None) =>
val description = generateDescription(desc)
val attributes = generateAttributes(properties.toSeq.flatten.sortBy(_._1), required.toSeq.flatten)
val caseClass = s"""case class $className(
val caseClass = s"""import io.circe._
|import io.circe.generic.semiauto._
|
|case class $className(
| ${attributes.replace("\n", "\n ")}
|)""".stripMargin
|)
|
|object $className {
| implicit lazy val encoder: ObjectEncoder[$className] = deriveEncoder
| implicit lazy val decoder: Decoder[$className] = deriveDecoder
|}
|""".stripMargin
s"$description$caseClass"
case Definition(None, None, None, Some(t)) =>

case Definition(_, None, None, Some(t)) =>
val scalaType = swaggerToScalaType(t)
s"""import io.circe._
|
|case class $className(value: $scalaType) extends AnyVal
|
|object $className {
| implicit val encode: Encoder[$className] = obj => Json.from$scalaType(obj.value)
| implicit val decode: Decoder[$className] = _.as[$scalaType].map($className(_))
| implicit val encoder: Encoder[$className] = obj => Json.from$scalaType(obj.value)
| implicit val decoder: Decoder[$className] = _.as[$scalaType].map($className(_))
|}""".stripMargin
}

IO.write(file, s"""package $packageName
|
|$generatedClass""".stripMargin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import scala.util.{Failure, Success}

import akka.actor.ActorSystem
import akka.http.scaladsl.model._
import io.circe.generic.auto._
import io.circe._
import io.circe.parser._
import io.k8s.apimachinery.pkg.apis.meta.v1.{DeleteOptions, ObjectMeta}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.goyeau.kubernetes.client

import akka.actor.ActorSystem
import io.circe.generic.auto._

case class KubernetesClient(config: KubeConfig)(implicit system: ActorSystem) {
lazy val namespaces = NamespacesOperations(config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import akka.stream.scaladsl.BidiFlow
import akka.stream.scaladsl.Flow
import akka.util.ByteString
import io.circe._
import io.circe.generic.auto._
import io.circe.parser.decode
import io.k8s.api.core.v1.{Pod, PodList}
import io.k8s.apimachinery.pkg.apis.meta.v1.Status
Expand Down
23 changes: 21 additions & 2 deletions src/main/scala/com/goyeau/kubernetes/client/YamlUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.goyeau.kubernetes.client
import java.io.File

import scala.io.Source

import com.typesafe.scalalogging.LazyLogging
import io.circe.generic.auto._
import io.circe.{Decoder, ObjectEncoder}
import io.circe.generic.semiauto._
import io.circe.yaml.parser

case class Config(
Expand Down Expand Up @@ -71,4 +71,23 @@ object YamlUtils extends LazyLogging {
error => throw new IllegalArgumentException(s"Parsing config file $kubeconfig failed: ${error.getMessage}"),
identity
)

implicit lazy val configDecoder: Decoder[Config] = deriveDecoder
implicit lazy val configEncoder: ObjectEncoder[Config] = deriveEncoder

implicit lazy val clusterDecoder: Decoder[Cluster] = deriveDecoder
implicit lazy val clusterEncoder: ObjectEncoder[Cluster] = deriveEncoder
implicit lazy val namedClusterDecoder: Decoder[NamedCluster] = deriveDecoder
implicit lazy val namedClusterEncoder: ObjectEncoder[NamedCluster] = deriveEncoder

implicit lazy val contextDecoder: Decoder[Context] = deriveDecoder
implicit lazy val contextEncoder: ObjectEncoder[Context] = deriveEncoder
implicit lazy val namedContextDecoder: Decoder[NamedContext] = deriveDecoder
implicit lazy val namedContextEncoder: ObjectEncoder[NamedContext] = deriveEncoder

implicit lazy val authInfoDecoder: Decoder[AuthInfo] = deriveDecoder
implicit lazy val authInfoEncoder: ObjectEncoder[AuthInfo] = deriveEncoder
implicit lazy val namedAuthInfoDecoder: Decoder[NamedAuthInfo] = deriveDecoder
implicit lazy val namedAuthInfoEncoder: ObjectEncoder[NamedAuthInfo] = deriveEncoder

}
Loading

0 comments on commit 5ad9ac3

Please sign in to comment.