-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade libraries Move shapeless code into scala-2 source directory Use scala.derving.Mirror for scala-3 code
- Loading branch information
Showing
14 changed files
with
233 additions
and
89 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 |
---|---|---|
|
@@ -12,3 +12,7 @@ project/plugins/project/ | |
# Vagrant | ||
.vagrant | ||
VERSION | ||
.bloop | ||
.metals | ||
metals.sbt | ||
.vscode |
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 |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.4.1 | ||
sbt.version=1.8.2 |
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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.5.0") | ||
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.9.3") | ||
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.1.1") | ||
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.7") | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-site" % "1.4.0") | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.0") | ||
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.0") | ||
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.7") | ||
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.9.2") | ||
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.3") | ||
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6") | ||
addSbtPlugin("com.dwijnand" % "sbt-dynver" % "4.1.1") | ||
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.10.0-RC1") | ||
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.10.0-RC1") | ||
|
||
libraryDependencySchemes += "org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always |
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
49 changes: 49 additions & 0 deletions
49
...cala-2/com.snowplowanalytics.snowplow.analytics.scalasdk/decode/RowDecoderCompanion.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,49 @@ | ||
/* | ||
* Copyright (c) 2016-2019 Snowplow Analytics Ltd. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package com.snowplowanalytics.snowplow.analytics.scalasdk.decode | ||
|
||
import shapeless._ | ||
import cats.syntax.validated._ | ||
import cats.syntax.either._ | ||
import cats.syntax.apply._ | ||
import com.snowplowanalytics.snowplow.analytics.scalasdk.ParsingError.RowDecodingErrorInfo.UnhandledRowDecodingError | ||
|
||
private[scalasdk] trait RowDecoderCompanion { | ||
import HList.ListCompat._ | ||
|
||
def apply[L <: HList](implicit fromRow: RowDecoder[L]): RowDecoder[L] = fromRow | ||
|
||
def fromFunc[L <: HList](f: List[(Key, String)] => RowDecodeResult[L]): RowDecoder[L] = | ||
new RowDecoder[L] { | ||
def apply(row: List[(Key, String)]) = f(row) | ||
} | ||
|
||
/** Parse TSV row into HList */ | ||
private def parse[H: ValueDecoder, T <: HList: RowDecoder](row: List[(Key, String)]): RowDecodeResult[H :: T] = | ||
row match { | ||
case h :: t => | ||
val hv: RowDecodeResult[H] = ValueDecoder[H].parse(h).toValidatedNel | ||
val tv: RowDecodeResult[T] = RowDecoder[T].apply(t) | ||
(hv, tv).mapN(_ :: _) | ||
case Nil => UnhandledRowDecodingError("Not enough values, format is invalid").invalidNel | ||
} | ||
|
||
implicit def hnilFromRow: RowDecoder[HNil] = | ||
fromFunc { | ||
case Nil => HNil.validNel | ||
case rows => UnhandledRowDecodingError(s"No more values expected, following provided: ${rows.map(_._2).mkString(", ")}").invalidNel | ||
} | ||
|
||
implicit def hconsFromRow[H: ValueDecoder, T <: HList: RowDecoder]: RowDecoder[H :: T] = | ||
fromFunc(row => parse(row)) | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/scala-3/com.snowplowanalytics.snowplow.analytics.scalasdk/decode/Parser.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,50 @@ | ||
/* | ||
* Copyright (c) 2016-2019 Snowplow Analytics Ltd. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package com.snowplowanalytics.snowplow.analytics.scalasdk.decode | ||
|
||
import cats.data.{NonEmptyList, Validated} | ||
import com.snowplowanalytics.snowplow.analytics.scalasdk.ParsingError.{FieldNumberMismatch, NotTSV, RowDecodingError} | ||
import scala.deriving._ | ||
import scala.compiletime._ | ||
|
||
private[scalasdk] trait Parser[A] extends Serializable { | ||
|
||
/** List of field names defined on `A` */ | ||
def knownKeys: List[Key] // TODO: should be a part of `RowDecoder` | ||
|
||
protected def decoder: RowDecoder[A] | ||
|
||
def parse(row: String): DecodeResult[A] = { | ||
val values = row.split("\t", -1) | ||
if (values.length == 1) Validated.Invalid(NotTSV) | ||
else if (values.length != knownKeys.length) Validated.Invalid(FieldNumberMismatch(values.length)) | ||
else { | ||
val zipped = knownKeys.zip(values) | ||
decoder(zipped).leftMap(e => RowDecodingError(e)) | ||
} | ||
} | ||
} | ||
|
||
object Parser { | ||
sealed trait DeriveParser[A] { | ||
inline def get(implicit mirror: Mirror.ProductOf[A]): Parser[A] = | ||
new Parser[A] { | ||
val knownKeys: List[Symbol] = constValueTuple[mirror.MirroredElemLabels].toArray.map(s => Symbol(s.toString)).toList | ||
val decoder: RowDecoder[A] = RowDecoder.of[A] | ||
} | ||
} | ||
|
||
/** Derive a TSV parser for `A` */ | ||
private[scalasdk] def deriveFor[A]: DeriveParser[A] = | ||
new DeriveParser[A] {} | ||
} |
54 changes: 54 additions & 0 deletions
54
...cala-3/com.snowplowanalytics.snowplow.analytics.scalasdk/decode/RowDecoderCompanion.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,54 @@ | ||
/* | ||
* Copyright (c) 2016-2019 Snowplow Analytics Ltd. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, | ||
* and you may not use this file except in compliance with the Apache License Version 2.0. | ||
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the Apache License Version 2.0 is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
*/ | ||
package com.snowplowanalytics.snowplow.analytics.scalasdk.decode | ||
|
||
import cats.syntax.validated._ | ||
import cats.syntax.either._ | ||
import cats.syntax.apply._ | ||
import com.snowplowanalytics.snowplow.analytics.scalasdk.ParsingError.RowDecodingErrorInfo.UnhandledRowDecodingError | ||
import scala.deriving._ | ||
import scala.compiletime._ | ||
|
||
private[scalasdk] trait RowDecoderCompanion { | ||
def tupled[L <: Tuple](implicit fromRow: RowDecoder[L]): RowDecoder[L] = fromRow | ||
|
||
def fromFunc[L <: Tuple](f: List[(Key, String)] => RowDecodeResult[L]): RowDecoder[L] = | ||
new RowDecoder[L] { | ||
def apply(row: List[(Key, String)]) = f(row) | ||
} | ||
|
||
/** Parse TSV row into HList */ | ||
private def parse[H: ValueDecoder, T <: Tuple: RowDecoder](row: List[(Key, String)]): RowDecodeResult[H *: T] = | ||
row match { | ||
case h :: t => | ||
val hv: RowDecodeResult[H] = ValueDecoder[H].parse(h).toValidatedNel | ||
val tv: RowDecodeResult[T] = RowDecoder.tupled[T].apply(t) | ||
(hv, tv).mapN(_ *: _) | ||
case Nil => UnhandledRowDecodingError("Not enough values, format is invalid").invalidNel | ||
} | ||
|
||
implicit def hnilFromRow: RowDecoder[EmptyTuple] = | ||
fromFunc { | ||
case Nil => EmptyTuple.validNel | ||
case rows => UnhandledRowDecodingError(s"No more values expected, following provided: ${rows.map(_._2).mkString(", ")}").invalidNel | ||
} | ||
|
||
implicit def hconsFromRow[H: ValueDecoder, T <: Tuple: RowDecoder]: RowDecoder[H *: T] = | ||
fromFunc(row => parse(row)) | ||
|
||
inline def of[A](implicit m: Mirror.ProductOf[A]) = { | ||
val instance = summonInline[RowDecoder[m.MirroredElemTypes]] | ||
instance.map(tuple => m.fromTuple(tuple)) | ||
} | ||
|
||
} |
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.