From e120eeae0b3a7ff75b95fd3542999a98b31fb323 Mon Sep 17 00:00:00 2001 From: ValdemarGr Date: Wed, 9 Aug 2023 14:54:16 +0200 Subject: [PATCH] drop validated support --- build.sbt | 13 ++++++++---- .../src/main/scala/spice4s/client/Touch.scala | 21 ------------------- .../scala/spice4s/client/models/Limit.scala | 2 +- .../spice4s/client/models/ZedToken.scala | 3 ++- .../scala/spice4s/client/util/package.scala | 20 ++++++++++-------- project/metals.sbt | 1 + project/plugins.sbt | 2 ++ .../protobuf/authzed/api/v1/package.proto | 9 ++++++++ .../protobuf/authzed/api/v1/package2.proto | 9 ++++++++ .../authzed/api/v1/permission_service.proto | 2 +- 10 files changed, 45 insertions(+), 37 deletions(-) delete mode 100644 client/src/main/scala/spice4s/client/Touch.scala create mode 100644 proto/src/main/protobuf/authzed/api/v1/package.proto create mode 100644 proto/src/main/protobuf/authzed/api/v1/package2.proto diff --git a/build.sbt b/build.sbt index 854cb67..58a1a84 100644 --- a/build.sbt +++ b/build.sbt @@ -32,15 +32,20 @@ lazy val proto = project .enablePlugins(Fs2Grpc) .settings( name := "spice4s-client-proto-api", - scalapbCodeGenerators := scalapbCodeGenerators.value ++ Seq( + scalapbCodeGenerators := scalapbCodeGenerators.value/* ++ Seq( protocbridge.Target(scalapb.validate.gen(), (Compile / sourceManaged).value / "scalapb") - ), + )*/, libraryDependencies ++= Seq( - "com.thesamet.scalapb" %% "scalapb-validate-core" % scalapb.validate.compiler.BuildInfo.version % "protobuf", + /* "com.thesamet.scalapb" %% "scalapb-validate-core" % scalapb.validate.compiler.BuildInfo.version % "protobuf", */ + "com.thesamet.scalapb.common-protos" %% "pgv-proto-scalapb_0.11" % "0.6.13-0", + "com.thesamet.scalapb.common-protos" %% "pgv-proto-scalapb_0.11" % "0.6.13-0" % "protobuf", "com.thesamet.scalapb.common-protos" %% "proto-google-common-protos-scalapb_0.11" % "2.9.6-0" % "protobuf", "com.thesamet.scalapb.common-protos" %% "proto-google-common-protos-scalapb_0.11" % "2.9.6-0" ), - tlFatalWarningsInCi := false + tlFatalWarningsInCi := false, + excludeDependencies ++= Seq( + ExclusionRule("com.thesamet.scalapb", "scalapb-validate-core_2.13"), + ) ) lazy val client = project diff --git a/client/src/main/scala/spice4s/client/Touch.scala b/client/src/main/scala/spice4s/client/Touch.scala deleted file mode 100644 index 1ace74b..0000000 --- a/client/src/main/scala/spice4s/client/Touch.scala +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2023 CaseHubDK - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package spice4s.client - -object Main extends App { - println("Hello, world!") -} diff --git a/client/src/main/scala/spice4s/client/models/Limit.scala b/client/src/main/scala/spice4s/client/models/Limit.scala index 98426b7..a325061 100644 --- a/client/src/main/scala/spice4s/client/models/Limit.scala +++ b/client/src/main/scala/spice4s/client/models/Limit.scala @@ -26,5 +26,5 @@ object Limit { def apply(value: Int): Validation[Limit] = if (value >= 0 || value <= 1000) unsafeFromString(value).valid - else invalid("limit", value, "Limit must be between 0 and 1000") + else invalid("limit", "Limit must be between 0 and 1000") } diff --git a/client/src/main/scala/spice4s/client/models/ZedToken.scala b/client/src/main/scala/spice4s/client/models/ZedToken.scala index 9cd2558..9dc297b 100644 --- a/client/src/main/scala/spice4s/client/models/ZedToken.scala +++ b/client/src/main/scala/spice4s/client/models/ZedToken.scala @@ -28,7 +28,8 @@ object ZedToken { def unsafeFromString(token: String): ZedToken = new ZedToken(token) {} def apply(token: String): Validation[ZedToken] = - unsafeFromString(token).validator(_.encode) + if (token.size >= 1) unsafeFromString(token).valid + else invalid("zed-token", "ZedToken must be at least 1 character long") def decode(x: core.ZedToken): Decoded[Option[ZedToken]] = opt(x.token).map(unsafeFromString).pure[Decoded] diff --git a/client/src/main/scala/spice4s/client/util/package.scala b/client/src/main/scala/spice4s/client/util/package.scala index a3f364c..f4dbb69 100644 --- a/client/src/main/scala/spice4s/client/util/package.scala +++ b/client/src/main/scala/spice4s/client/util/package.scala @@ -17,34 +17,36 @@ package spice4s.client import cats._ -import scalapb.validate._ import cats.data._ import cats.implicits._ import scala.util.matching.Regex import cats.ApplicativeError package object util { + final case class ValidationFailure(name: String, message: String) + implicit class AnyUtilOps[A](private val value: A) { - def validator[V: Validator](f: A => V): Validated[List[ValidationFailure], A] = - Validator[V].validate(f(value)) match { - case Failure(violations) => violations.invalid - case Success => value.valid - } + def minSize(size: Int)(f: A => String): Validated[List[ValidationFailure], A] = + if (f(value).length >= size) value.valid + else + invalid( + "min-size-field", + s"'$value' with encoded value ${f(value)} is less than min size $size" + ) def validateRegex(regex: Regex)(f: A => String): Validated[List[ValidationFailure], A] = if (regex.matches(f(value))) value.valid else invalid( "regex-field", - f(value), s"$value with encoded value ${f(value)} does not match regex ${regex.toString}" ) } type Validation[A] = Validated[List[ValidationFailure], A] - def invalid[A](name: String, a: Any, reason: String): Validation[A] = - List(ValidationFailure(name, a.toString, reason)).invalid + def invalid[A](name: String, message: String): Validation[A] = + List(ValidationFailure(name, message)).invalid final case class DecoderError(message: String, path: Chain[String]) { def prepend(p: String): DecoderError = copy(path = p +: path) diff --git a/project/metals.sbt b/project/metals.sbt index a3a8769..05fd2b3 100644 --- a/project/metals.sbt +++ b/project/metals.sbt @@ -3,3 +3,4 @@ // This file enables sbt-bloop to create bloop config files. addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.5.6") + diff --git a/project/plugins.sbt b/project/plugins.sbt index 16002ee..0f57a8f 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -7,3 +7,5 @@ libraryDependencies ++= Seq( "com.thesamet.scalapb" %% "compilerplugin" % "0.11.11", "com.thesamet.scalapb" %% "scalapb-validate-codegen" % "0.3.4" ) + +addDependencyTreePlugin diff --git a/proto/src/main/protobuf/authzed/api/v1/package.proto b/proto/src/main/protobuf/authzed/api/v1/package.proto new file mode 100644 index 0000000..123fba9 --- /dev/null +++ b/proto/src/main/protobuf/authzed/api/v1/package.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; +import "scalapb/scalapb.proto"; + +package google.api; + +option (scalapb.options) = { + scope: PACKAGE + flat_package: false +}; diff --git a/proto/src/main/protobuf/authzed/api/v1/package2.proto b/proto/src/main/protobuf/authzed/api/v1/package2.proto new file mode 100644 index 0000000..704d503 --- /dev/null +++ b/proto/src/main/protobuf/authzed/api/v1/package2.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; +import "scalapb/scalapb.proto"; + +package authzed.api; + +option (scalapb.options) = { + scope: PACKAGE + flat_package: false +}; diff --git a/proto/src/main/protobuf/authzed/api/v1/permission_service.proto b/proto/src/main/protobuf/authzed/api/v1/permission_service.proto index 05a8569..f181b59 100644 --- a/proto/src/main/protobuf/authzed/api/v1/permission_service.proto +++ b/proto/src/main/protobuf/authzed/api/v1/permission_service.proto @@ -545,4 +545,4 @@ message ResolvedSubject { // partial_caveat_info holds information of a partially-evaluated caveated response PartialCaveatInfo partial_caveat_info = 3 [ (validate.rules).message.required = false ]; -} \ No newline at end of file +}