Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Introduce
Value[A]
and extract tapir and zio-json codecs (#…
- Loading branch information
Showing
25 changed files
with
232 additions
and
222 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
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
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
72 changes: 72 additions & 0 deletions
72
webapi/src/main/scala/org/knora/webapi/slice/admin/api/Codecs.scala
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,72 @@ | ||
/* | ||
* Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.knora.webapi.slice.admin.api | ||
|
||
import sttp.tapir.Codec | ||
import sttp.tapir.CodecFormat | ||
import zio.json.JsonCodec | ||
|
||
import dsp.valueobjects.V2 | ||
import org.knora.webapi.slice.admin.api.model.MaintenanceRequests.AssetId | ||
import org.knora.webapi.slice.admin.domain.model.KnoraProject.* | ||
import org.knora.webapi.slice.common.Value.BooleanValue | ||
import org.knora.webapi.slice.common.Value.StringValue | ||
import org.knora.webapi.slice.common.domain.SparqlEncodedString | ||
|
||
object Codecs { | ||
object TapirCodec { | ||
|
||
private type StringCodec[A] = Codec[String, A, CodecFormat.TextPlain] | ||
private def stringCodec[A <: StringValue](from: String => Either[String, A]): StringCodec[A] = | ||
stringCodec(from, _.value) | ||
private def stringCodec[A](from: String => Either[String, A], to: A => String): StringCodec[A] = | ||
Codec.string.mapEither(from)(to) | ||
|
||
private def booleanCodec[A <: BooleanValue](from: Boolean => A): StringCodec[A] = | ||
booleanCodec(from, _.value) | ||
private def booleanCodec[A](from: Boolean => A, to: A => Boolean): StringCodec[A] = | ||
Codec.boolean.map(from)(to) | ||
|
||
implicit val assetId: StringCodec[AssetId] = stringCodec(AssetId.from, _.value) | ||
implicit val keyword: StringCodec[Keyword] = stringCodec(Keyword.from) | ||
implicit val logo: StringCodec[Logo] = stringCodec(Logo.from) | ||
implicit val longname: StringCodec[Longname] = stringCodec(Longname.from) | ||
implicit val projectIri: StringCodec[ProjectIri] = stringCodec(ProjectIri.from) | ||
implicit val selfJoin: StringCodec[SelfJoin] = booleanCodec(SelfJoin.from) | ||
implicit val shortcode: StringCodec[Shortcode] = stringCodec(Shortcode.from) | ||
implicit val shortname: StringCodec[Shortname] = stringCodec(Shortname.from) | ||
implicit val sparqlEncodedString: StringCodec[SparqlEncodedString] = stringCodec(SparqlEncodedString.from) | ||
implicit val status: StringCodec[Status] = booleanCodec(Status.from) | ||
} | ||
|
||
object ZioJsonCodec { | ||
|
||
private type StringCodec[A] = JsonCodec[A] | ||
private def stringCodec[A <: StringValue](from: String => Either[String, A]): StringCodec[A] = | ||
stringCodec(from, _.value) | ||
private def stringCodec[A](from: String => Either[String, A], to: A => String): StringCodec[A] = | ||
JsonCodec[String].transformOrFail(from, to) | ||
|
||
private def booleanCodec[A <: BooleanValue](from: Boolean => A): StringCodec[A] = | ||
booleanCodec(from, _.value) | ||
private def booleanCodec[A](from: Boolean => A, to: A => Boolean): StringCodec[A] = | ||
JsonCodec[Boolean].transform(from, to) | ||
|
||
implicit val description: StringCodec[Description] = | ||
JsonCodec[V2.StringLiteralV2].transformOrFail(Description.from, _.value) | ||
|
||
implicit val assetId: StringCodec[AssetId] = stringCodec(AssetId.from, _.value) | ||
implicit val keyword: StringCodec[Keyword] = stringCodec(Keyword.from) | ||
implicit val logo: StringCodec[Logo] = stringCodec(Logo.from) | ||
implicit val longname: StringCodec[Longname] = stringCodec(Longname.from) | ||
implicit val projectIri: StringCodec[ProjectIri] = stringCodec(ProjectIri.from) | ||
implicit val selfJoin: StringCodec[SelfJoin] = booleanCodec(SelfJoin.from) | ||
implicit val shortcode: StringCodec[Shortcode] = stringCodec(Shortcode.from) | ||
implicit val shortname: StringCodec[Shortname] = stringCodec(Shortname.from) | ||
implicit val sparqlEncodedString: StringCodec[SparqlEncodedString] = stringCodec(SparqlEncodedString.from) | ||
implicit val status: StringCodec[Status] = booleanCodec(Status.from) | ||
} | ||
} |
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
Oops, something went wrong.