Skip to content

Commit

Permalink
fmt and fix all
Browse files Browse the repository at this point in the history
  • Loading branch information
ThijsBroersen committed Nov 16, 2024
1 parent d041e60 commit cf46d34
Show file tree
Hide file tree
Showing 33 changed files with 372 additions and 397 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package zio.json.golden

import java.io.{ File, IOException }
import java.nio.file.{ Path }
import zio.{ test => _, _ }
import zio.json._

import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.{ test => _, _ }

import java.nio.file.Files
import java.io.{ File, IOException }
import java.nio.file.{ Files, Path }

object filehelpers {

Expand Down
11 changes: 4 additions & 7 deletions zio-json-golden/src/main/scala/zio/json/golden/package.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package zio.json

import scala.annotation.nowarn

import zio.Tag
import zio.{ test => _, _ }
import zio.json.ast._
import zio.json.golden.filehelpers._
import zio.stacktracer.TracingImplicits.disableAutoTrace
import zio.test._
import zio.test.diff._
import zio.test.diff.Diff._
import java.nio.file.{ Paths, Path, Files }
import zio.test.diff._
import zio.{ Tag, test => _, _ }

import zio.json.ast._
import java.nio.file.{ Files, Path, Paths }

package object golden {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zio.json.interop

import eu.timepit.refined.api.{ Refined, Validate }
import eu.timepit.refined.{ refineV }
import eu.timepit.refined.refineV
import zio.json._

package object refined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ private[json] final class DeriveCodecMacros(val c: blackbox.Context) {
private[this] val EncoderClass = typeOf[JsonEncoder[_]].typeSymbol.asType
private[this] val CodecClass = typeOf[JsonCodec[_]].typeSymbol.asType

private[this] val macroName: Tree = {
private[this] val macroName: Tree =
c.prefix.tree match {
case Apply(Select(New(name), _), _) => name
case _ => c.abort(c.enclosingPosition, "Unexpected macro application")
}
}

private[this] val (codecStyle: JsonCodecStyle, codecType: JsonCodecType) = {
val style: JsonCodecStyle = macroName match {
Expand Down
6 changes: 2 additions & 4 deletions zio-json-yaml/src/main/scala/zio/json/yaml/YamlOptions.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package zio.json.yaml

import org.yaml.snakeyaml.DumperOptions.{ FlowStyle, LineBreak, NonPrintableStyle, ScalarStyle }
import org.yaml.snakeyaml.DumperOptions

import org.yaml.snakeyaml.DumperOptions.{ FlowStyle, LineBreak, NonPrintableStyle, ScalarStyle }
import zio.json.ast.Json

case class YamlOptions(
Expand All @@ -20,11 +19,10 @@ case class YamlOptions(
)

object YamlOptions {
private val defaultLineBreak: LineBreak = {
private val defaultLineBreak: LineBreak =
Set(LineBreak.MAC, LineBreak.WIN, LineBreak.UNIX)
.find(_.getString == System.lineSeparator())
.getOrElse(LineBreak.UNIX)
}

val default: YamlOptions = YamlOptions(
() => new DumperOptions(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package zio.json.yaml.internal

import org.yaml.snakeyaml.LoaderOptions
import org.yaml.snakeyaml.constructor.SafeConstructor
import org.yaml.snakeyaml.nodes.{ MappingNode, Node }
import org.yaml.snakeyaml.LoaderOptions

private[yaml] final class YamlValueConstruction extends SafeConstructor(new LoaderOptions()) {
def toJavaValue(node: Node): AnyRef =
Expand Down
2 changes: 1 addition & 1 deletion zio-json-yaml/src/main/scala/zio/json/yaml/package.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package zio.json

import org.yaml.snakeyaml.DumperOptions.{ NonPrintableStyle, ScalarStyle }
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.emitter.Emitter
import org.yaml.snakeyaml.error.YAMLException
import org.yaml.snakeyaml.nodes.{ ScalarNode, _ }
import org.yaml.snakeyaml.reader.StreamReader
import org.yaml.snakeyaml.resolver.Resolver
import org.yaml.snakeyaml.serializer._
import org.yaml.snakeyaml.Yaml
import zio.Chunk
import zio.json.ast.Json
import zio.json.yaml.internal.YamlValueConstruction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ trait JsonDecoderPlatformSpecific[A] { self: JsonDecoder[A] =>
}

/**
* Attempts to decode a stream of bytes using the user supplied Charset into a single value of type `A`, but may fail with
* a human-readable exception if the stream does not encode a value of this type.
* Attempts to decode a stream of bytes using the user supplied Charset into a single value of type `A`, but may fail
* with a human-readable exception if the stream does not encode a value of this type.
*
* Note: This method may not consume the full string.
*
* @see [[decodeJsonStream]] For a `Char` stream variant
* @see
* [[decodeJsonStream]] For a `Char` stream variant
*/
final def decodeJsonStreamInput[R](
stream: ZStream[R, Throwable, Byte],
Expand All @@ -41,12 +42,13 @@ trait JsonDecoderPlatformSpecific[A] { self: JsonDecoder[A] =>
}

/**
* Attempts to decode a stream of characters into a single value of type `A`, but may fail with
* a human-readable exception if the stream does not encode a value of this type.
* Attempts to decode a stream of characters into a single value of type `A`, but may fail with a human-readable
* exception if the stream does not encode a value of this type.
*
* Note: This method may not consume the full string.
*
* @see also [[decodeJsonStreamInput]]
* @see
* also [[decodeJsonStreamInput]]
*/
final def decodeJsonStream[R](stream: ZStream[R, Throwable, Char]): ZIO[R, Throwable, A] =
ZIO.scoped[R](stream.toReader.flatMap(readAll))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.nio.file.Paths

object DecoderPlatformSpecificSpec extends ZIOSpecDefault {

val spec =
val spec: Spec[Annotations with Live with Sized with TestConfig, Throwable] =
suite("Decoder")(
test("excessively nested structures") {
// JVM specific: getResourceAsString not yet supported
Expand Down Expand Up @@ -273,7 +273,7 @@ object DecoderPlatformSpecificSpec extends ZIOSpecDefault {
)
)

def testAst(label: String) =
def testAst(label: String): Spec[Any, Throwable] =
test(label) {
getResourceAsStringM(s"jawn/$label.json").flatMap { input =>
val expected = jawn.JParser.parseFromString(input).toEither.map(fromJawn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import zio.Chunk
import zio.json.ast.Json
import zio.stream.{ ZSink, ZStream }
import zio.test.Assertion._
import zio.test.{ ZIOSpecDefault, assert, _ }
import zio.test.{ Spec, ZIOSpecDefault, assert, _ }

import java.io.IOException
import java.nio.file.Files

object EncoderPlatformSpecificSpec extends ZIOSpecDefault {
import testzio.json.DecoderSpec.logEvent._

val spec =
val spec: Spec[Any, Throwable] =
suite("Encoder")(
suite("roundtrip")(
testRoundTrip[DistanceMatrix]("google_maps_api_response"),
Expand Down
6 changes: 3 additions & 3 deletions zio-json/jvm/src/test/scala/zio/json/TestUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ object TestUtils {
def getResourceAsString(res: String): String = {
val is = getClass.getClassLoader.getResourceAsStream(res)
try {
val baos = new java.io.ByteArrayOutputStream()
val data = Array.ofDim[Byte](2048)
var len: Int = 0
val baos = new java.io.ByteArrayOutputStream()
val data = Array.ofDim[Byte](2048)
var len: Int = 0
def read(): Int = { len = is.read(data); len }
while (read() != -1)
baos.write(data, 0, len)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package testzio.json.internal

import testzio.json.Gens._
import zio.Scope
import zio.json.internal._
import zio.test.Assertion._
import zio.test._

object SafeNumbersSpec extends ZIOSpecDefault {
val spec =
val spec: Spec[Environment with TestEnvironment with Scope, Any] =
suite("SafeNumbers")(
test("valid big decimals") {
check(genBigDecimal)(i => assert(SafeNumbers.bigDecimal(i.toString, 2048))(isSome(equalTo(i))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ object StringMatrixSpec extends ZIOSpecDefault {
}
)

val genNonEmptyString =
val genNonEmptyString: Gen[Any, String] =
Gen.alphaNumericString.filter(_.nonEmpty)

val genTestStrings =
val genTestStrings: Gen[Any, List[String]] =
for {
n <- Gen.int(1, 63)
xs <- Gen.setOfN(n)(genNonEmptyString)
} yield xs.toList

val genTestStringsAndAliases =
val genTestStringsAndAliases: Gen[Any, (List[String], List[(String, Int)])] =
for {
xsn <- Gen.int(1, 63)
xs <- Gen.setOfN(xsn)(genNonEmptyString)
Expand Down
74 changes: 34 additions & 40 deletions zio-json/shared/src/main/scala-2.x/zio/json/macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import scala.annotation._
import scala.language.experimental.macros

/**
* If used on a case class field, determines the name of the JSON field.
* Defaults to the case class field name.
* If used on a case class field, determines the name of the JSON field. Defaults to the case class field name.
*/
final case class jsonField(name: String) extends Annotation

Expand All @@ -25,17 +24,14 @@ final case class jsonAliases(alias: String, aliases: String*) extends Annotation
final class jsonExplicitNull extends Annotation

/**
* If used on a sealed class, will determine the name of the field for
* disambiguating classes.
* If used on a sealed class, will determine the name of the field for disambiguating classes.
*
* The default is to not use a typehint field and instead
* have an object with a single key that is the class name.
* The default is to not use a typehint field and instead have an object with a single key that is the class name.
*
* Note that using a discriminator is less performant, uses more memory, and may
* be prone to DOS attacks that are impossible with the default encoding. In
* addition, there is slightly less type safety when using custom product
* encoders (which must write an unenforced object type). Only use this option
* if you must model an externally defined schema.
* Note that using a discriminator is less performant, uses more memory, and may be prone to DOS attacks that are
* impossible with the default encoding. In addition, there is slightly less type safety when using custom product
* encoders (which must write an unenforced object type). Only use this option if you must model an externally defined
* schema.
*/
final case class jsonDiscriminator(name: String) extends Annotation
// TODO a strategy where the constructor is inferred from the field names, only
Expand Down Expand Up @@ -80,16 +76,14 @@ object ziojson_03 {
}

/**
* If used on a case class, determines the strategy of member names
* transformation during serialization and deserialization. Four common
* strategies are provided above and a custom one to support specific use cases.
* If used on a case class, determines the strategy of member names transformation during serialization and
* deserialization. Four common strategies are provided above and a custom one to support specific use cases.
*/
final case class jsonMemberNames(format: JsonMemberFormat) extends Annotation
private[json] object jsonMemberNames {

/**
* ~~Stolen~~ Borrowed from jsoniter-scala by Andriy Plokhotnyuk
* (he even granted permission for this, imagine that!)
* ~~Stolen~~ Borrowed from jsoniter-scala by Andriy Plokhotnyuk (he even granted permission for this, imagine that!)
*/

import java.lang.Character._
Expand Down Expand Up @@ -172,27 +166,26 @@ private[json] object jsonMemberNames {
}

/**
* If used on a case class will determine the type hint value for disambiguating
* sealed traits. Defaults to the short type name.
* If used on a case class will determine the type hint value for disambiguating sealed traits. Defaults to the short
* type name.
*/
final case class jsonHint(name: String) extends Annotation

/**
* If used on a sealed class will determine the strategy of type hint value transformation for disambiguating
* classes during serialization and deserialization. Same strategies are provided as for [[jsonMemberNames]].
* If used on a sealed class will determine the strategy of type hint value transformation for disambiguating classes
* during serialization and deserialization. Same strategies are provided as for [[jsonMemberNames]].
*/
final case class jsonHintNames(format: JsonMemberFormat) extends Annotation

/**
* If used on a case class, will exit early if any fields are in the JSON that
* do not correspond to field names in the case class.
* If used on a case class, will exit early if any fields are in the JSON that do not correspond to field names in the
* case class.
*
* This adds extra protections against a DOS attacks but means that changes in
* the schema will result in a hard error rather than silently ignoring those
* fields.
* This adds extra protections against a DOS attacks but means that changes in the schema will result in a hard error
* rather than silently ignoring those fields.
*
* Cannot be combined with `@jsonDiscriminator` since it is considered an extra
* field from the perspective of the case class.
* Cannot be combined with `@jsonDiscriminator` since it is considered an extra field from the perspective of the case
* class.
*/
final class jsonNoExtraFields extends Annotation

Expand All @@ -205,10 +198,14 @@ final class jsonExclude extends Annotation
/**
* Implicit codec derivation configuration.
*
* @param sumTypeHandling see [[jsonDiscriminator]]
* @param fieldNameMapping see [[jsonMemberNames]]
* @param allowExtraFields see [[jsonNoExtraFields]]
* @param sumTypeMapping see [[jsonHintNames]]
* @param sumTypeHandling
* see [[jsonDiscriminator]]
* @param fieldNameMapping
* see [[jsonMemberNames]]
* @param allowExtraFields
* see [[jsonNoExtraFields]]
* @param sumTypeMapping
* see [[jsonHintNames]]
*/
final case class JsonCodecConfiguration(
sumTypeHandling: SumTypeHandling = WrapperWithClassNameField,
Expand All @@ -235,18 +232,15 @@ object JsonCodecConfiguration {
}

/**
* For sealed classes, will determine the name of the field for
* disambiguating classes.
* For sealed classes, will determine the name of the field for disambiguating classes.
*
* The default is to not use a typehint field and instead
* have an object with a single key that is the class name.
* The default is to not use a typehint field and instead have an object with a single key that is the class name.
* See [[WrapperWithClassNameField]].
*
* Note that using a discriminator is less performant, uses more memory, and may
* be prone to DOS attacks that are impossible with the default encoding. In
* addition, there is slightly less type safety when using custom product
* encoders (which must write an unenforced object type). Only use this option
* if you must model an externally defined schema.
* Note that using a discriminator is less performant, uses more memory, and may be prone to DOS attacks that are
* impossible with the default encoding. In addition, there is slightly less type safety when using custom product
* encoders (which must write an unenforced object type). Only use this option if you must model an externally
* defined schema.
*/
final case class DiscriminatorField(name: String) extends SumTypeHandling {
override def discriminatorField: Option[String] = Some(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ trait DecoderLowPriorityVersionSpecific {

inline given unionOfStringEnumeration[T](using IsUnionOf[String, T]): JsonDecoder[T] =
val values = UnionDerivation.constValueUnionTuple[String, T]
JsonDecoder.string.mapOrFail(
{
case raw if values.toList.contains(raw) => Right(raw.asInstanceOf[T])
case _ => Left("expected one of: " + values.toList.mkString(", "))
}
)
JsonDecoder.string.mapOrFail {
case raw if values.toList.contains(raw) => Right(raw.asInstanceOf[T])
case _ => Left("expected one of: " + values.toList.mkString(", "))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private[json] object IsUnionOf:

private def deriveImpl[T, A](using quotes: Quotes, t: Type[T], a: Type[A]): Expr[IsUnionOf[T, A]] =
import quotes.reflect.*
val tpe: TypeRepr = TypeRepr.of[A]
val tpe: TypeRepr = TypeRepr.of[A]
val bound: TypeRepr = TypeRepr.of[T]

def validateTypes(tpe: TypeRepr): Unit =
Expand Down
Loading

0 comments on commit cf46d34

Please sign in to comment.