Skip to content

Commit

Permalink
Merge pull request #172 from guardrail-dev/exposing-auth-implementation
Browse files Browse the repository at this point in the history
Exposing auth implementation
  • Loading branch information
blast-hardcheese authored Apr 20, 2022
2 parents 438b5b2 + 5a14f5c commit f1f838a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
6 changes: 6 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ ThisBuild / developers := List(
ThisBuild / scalaVersion := "2.12.15"
ThisBuild / scalacOptions ++= List("-feature", "-Xexperimental")

libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.11" % Test,
"org.scalacheck" %% "scalacheck" % "1.16.0" % Test,
"org.scalatestplus" %% "scalatestplus-scalacheck" % "3.1.0.0-RC2" % Test
)

// Versioning
enablePlugins(GitBranchPrompt)
enablePlugins(GitVersioning)
Expand Down
18 changes: 15 additions & 3 deletions modules/core/src/main/scala/AbstractCodegenPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.guardrail.runner.GuardrailRunner
import dev.guardrail.terms.protocol.PropertyRequirement
import dev.guardrail.{
Args => ArgsImpl,
AuthImplementation,
CodegenTarget => CodegenTargetImpl,
Context => ContextImpl
}
Expand All @@ -28,7 +29,8 @@ trait AbstractGuardrailPlugin extends GuardrailRunner { self: AutoPlugin =>
encodeOptionalAs: Option[CodingConfig],
decodeOptionalAs: Option[CodingConfig],
customExtraction: Option[Boolean],
tagsBehaviour: Option[ContextImpl.TagsBehaviour]
tagsBehaviour: Option[ContextImpl.TagsBehaviour],
authImplementation: Option[AuthImplementation]
): ArgsImpl = {
val propertyRequirement = (encodeOptionalAs, decodeOptionalAs) match {
case (None, None) => ContextImpl.empty.propertyRequirement
Expand All @@ -43,9 +45,10 @@ trait AbstractGuardrailPlugin extends GuardrailRunner { self: AutoPlugin =>
def kindaLens[A](member: Option[A])(proj: A => ContextImpl => ContextImpl): ContextImpl => ContextImpl = member.fold[ContextImpl => ContextImpl](identity _)(proj)

val contextTransforms = Seq[ContextImpl => ContextImpl](
kindaLens(authImplementation)(a => _.copy(authImplementation=a)),
kindaLens(customExtraction)(a => _.copy(customExtraction=a)),
kindaLens(tracing)(a => _.copy(tracing=a)),
kindaLens(tagsBehaviour)(a => _.copy(tagsBehaviour=a))
kindaLens(tagsBehaviour)(a => _.copy(tagsBehaviour=a)),
kindaLens(tracing)(a => _.copy(tracing=a))
)

ArgsImpl.empty.copy(
Expand Down Expand Up @@ -81,6 +84,7 @@ trait AbstractGuardrailPlugin extends GuardrailRunner { self: AutoPlugin =>
decodeOptionalAs: Keys.GuardrailConfigValue[CodingConfig] = Keys.Default,
customExtraction: Keys.GuardrailConfigValue[Boolean] = Keys.Default,
tagsBehaviour: Keys.GuardrailConfigValue[ContextImpl.TagsBehaviour] = Keys.Default,
authImplementation: Keys.GuardrailConfigValue[AuthImplementation] = Keys.Default,
): Types.Args = (language, impl(
kind = kind,
specPath = Some(specPath),
Expand All @@ -94,6 +98,7 @@ trait AbstractGuardrailPlugin extends GuardrailRunner { self: AutoPlugin =>
decodeOptionalAs = decodeOptionalAs.toOption,
customExtraction = customExtraction.toOption,
tagsBehaviour = tagsBehaviour.toOption,
authImplementation = authImplementation.toOption,
defaults = false
))

Expand All @@ -108,6 +113,7 @@ trait AbstractGuardrailPlugin extends GuardrailRunner { self: AutoPlugin =>
decodeOptionalAs: Keys.GuardrailConfigValue[CodingConfig] = Keys.Default,
customExtraction: Keys.GuardrailConfigValue[Boolean] = Keys.Default,
tagsBehaviour: Keys.GuardrailConfigValue[ContextImpl.TagsBehaviour] = Keys.Default,
authImplementation: Keys.GuardrailConfigValue[AuthImplementation] = Keys.Default,

// Deprecated parameters
packageName: Keys.GuardrailConfigValue[String] = Keys.Default,
Expand All @@ -125,6 +131,7 @@ trait AbstractGuardrailPlugin extends GuardrailRunner { self: AutoPlugin =>
decodeOptionalAs = decodeOptionalAs.toOption,
customExtraction = customExtraction.toOption,
tagsBehaviour = tagsBehaviour.toOption,
authImplementation = authImplementation.toOption,
defaults = true
))
}
Expand Down Expand Up @@ -170,6 +177,11 @@ trait AbstractGuardrailPlugin extends GuardrailRunner { self: AutoPlugin =>

def tagsAreIgnored = Keys.tagsAreIgnored
def tagsAsPackage = Keys.tagsAsPackage

def authImplementationDisable = Keys.authImplementationDisable
def authImplementationNative = Keys.authImplementationNative
def authImplementationSimple = Keys.authImplementationSimple
def authImplementationCustom = Keys.authImplementationCustom
}

private def cachedGuardrailTask(projectName: String, scope: String, scalaBinaryVersion: String)(kind: String, streams: _root_.sbt.Keys.TaskStreams)(tasks: List[(String, Args)], sources: Seq[java.io.File]) = {
Expand Down
7 changes: 6 additions & 1 deletion modules/core/src/main/scala/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.io.File
import _root_.sbt.{ SettingKey, TaskKey }
import scala.language.implicitConversions

import dev.guardrail.Context
import dev.guardrail.{AuthImplementation, Context}
import dev.guardrail.terms.protocol.PropertyRequirement

sealed trait CodingConfig {
Expand Down Expand Up @@ -45,4 +45,9 @@ object Keys {

def tagsAreIgnored: Context.TagsBehaviour = Context.TagsAreIgnored
def tagsAsPackage: Context.TagsBehaviour = Context.PackageFromTags

def authImplementationDisable: AuthImplementation = AuthImplementation.Disable
def authImplementationNative: AuthImplementation = AuthImplementation.Native
def authImplementationSimple: AuthImplementation = AuthImplementation.Simple
def authImplementationCustom: AuthImplementation = AuthImplementation.Custom
}
21 changes: 21 additions & 0 deletions src/test/scala/dev/guardrail/sbt/ContextParameterSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.guardrail.sbt

import dev.guardrail.Context

import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers

class EscapeTreeSpec extends AnyFunSuite with Matchers {

test("Ensure that all Context fields are accounted for") {
val Context(
framework,
customExtraction,
tracing,
modules,
propertyRequirement,
tagsBehaviour,
authImplementation
) = Context.empty
}
}

0 comments on commit f1f838a

Please sign in to comment.