Skip to content

Commit

Permalink
run scalafmt and fixed ci errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Yummy-Yums committed Sep 27, 2024
1 parent 51a7cf0 commit 268141e
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 88 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ lazy val root =
`zio-quickstart-prelude`,
`zio-quickstart-stm`,
`zio-quickstart-sql`,
`zio-quickstart-schema`,
`zio-quickstart-schema`
)
.settings(
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
Expand All @@ -63,4 +63,4 @@ lazy val `zio-quickstart-cache` = project
lazy val `zio-quickstart-prelude` = project
lazy val `zio-quickstart-stm` = project
lazy val `zio-quickstart-sql` = project
lazy val `zio-quickstart-schema` = project
lazy val `zio-quickstart-schema` = project
19 changes: 9 additions & 10 deletions zio-quickstart-schema/build.sbt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
scalaVersion := "2.13.12"
organization := "dev.zio"
name := "zio-quickstart-restful-schema"

name := "zio-quickstart-schema"

libraryDependencies ++= Seq(
"dev.zio" %% "zio-schema" % "1.4.1",
"dev.zio" %% "zio-schema-zio-test" % "1.4.1",
"dev.zio" %% "zio-schema-derivation" % "1.4.1",
"org.scala-lang" % "scala-reflect" % scalaVersion.value % "provided",
"dev.zio" %% "zio-test" % "2.1.9" % Test,
"dev.zio" %% "zio-test-sbt" % "2.1.9" % Test,
"dev.zio" %% "zio-test-magnolia" % "2.1.9" % Test
"dev.zio" %% "zio-schema" % "1.4.1",
"dev.zio" %% "zio-schema-zio-test" % "1.4.1",
"dev.zio" %% "zio-schema-derivation" % "1.4.1",
"org.scala-lang" % "scala-reflect" % scalaVersion.value % "provided",
"dev.zio" %% "zio-test" % "2.1.9" % Test,
"dev.zio" %% "zio-test-sbt" % "2.1.9" % Test,
"dev.zio" %% "zio-test-magnolia" % "2.1.9" % Test
)

resolvers ++= Resolver.sonatypeOssRepos("snapshots")
resolvers ++= Resolver.sonatypeOssRepos("snapshots")
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import zio.schema.validation.Validation

// one can choose the detailed way of validation or use annotations

case class Person(name: String,
@validate(Validation.greaterThan(18))
age: Int)
case class Person(
name: String,
@validate(Validation.greaterThan(18))
age: Int
)

object Person {
implicit val schema: Schema[Person] = DeriveSchema.gen
}
implicit val schema: Schema[Person] = DeriveSchema.gen
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ case class PersonWithEmail(email: String)
object PersonWithEmail {

val localPart = Regex.letter.atLeast(3)
val tld = (Regex.literal("org") | Regex.literal("net") | Regex.literal("com")) // Matches top-level domains (2 or more letters)
val regexValidator = localPart ~ Regex.digit.atLeast(1) ~ Regex.literal("@") ~ Regex.letter.atLeast(3) ~ Regex.literal(".") ~ tld
val tld =
(Regex.literal("org") | Regex.literal("net") | Regex.literal(
"com"
)) // Matches top-level domains (2 or more letters)
val regexValidator = localPart ~ Regex.digit.atLeast(1) ~ Regex.literal(
"@"
) ~ Regex.letter.atLeast(3) ~ Regex.literal(".") ~ tld

implicit val schema: Schema[PersonWithEmail] = CaseClass1(
id0 = TypeId.fromTypeName("PersonWithEmail"),
Expand All @@ -24,7 +29,7 @@ object PersonWithEmail {
get0 = (p: PersonWithEmail) => p.email,
set0 = { (p: PersonWithEmail, s: String) => p.copy(email = s) }
),
defaultConstruct0 = email => PersonWithEmail(email),
defaultConstruct0 = email => PersonWithEmail(email)
)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,36 @@ object PrepareDataUtils {

def withFile[A](name: String)(useFile: BufferedSource => Task[A]): Task[A] =
ZIO.acquireReleaseWith(openFile(name))(closeFile)(useFile)
def getNameAndAgeOfPerson(bufferedSourceFile: BufferedSource): List[Person] = {
def getNameAndAgeOfPerson(
bufferedSourceFile: BufferedSource
): List[Person] = {

def getPerson: Iterator[Person] = for {
line <- bufferedSourceFile.getLines().filter(incomingString => !incomingString.contains("Name"))
list = line.split(",")
dob = list.reverse.head
name = list.head
age = getAge(dob)
line <- bufferedSourceFile
.getLines()
.filter(incomingString => !incomingString.contains("Name"))
list = line.split(",")
dob = list.reverse.head
name = list.head
age = getAge(dob)
person = Person(name, age)
} yield person

getPerson.toList

}

def getEmailOfPerson(bufferedSourceFile: BufferedSource): List[PersonWithEmail] = {
def getEmailOfPerson(
bufferedSourceFile: BufferedSource
): List[PersonWithEmail] = {

def getPersonWithEmail: Iterator[PersonWithEmail] = for {
line <- bufferedSourceFile.getLines().filter(incomingString => !incomingString.contains("Name"))
arr = line.split(",")
line <- bufferedSourceFile
.getLines()
.filter(incomingString => !incomingString.contains("Name"))
arr = line.split(",")
emaii = arr(1)


personWithEmail = PersonWithEmail(emaii)
} yield personWithEmail

Expand All @@ -54,6 +61,4 @@ object PrepareDataUtils {
currYear - LocalDate.parse(dob).getYear
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@ import zio.schema.annotation.validate
import zio.schema.validation.Validation
import zio.stream.ZStream


object SchemaValidator extends ZIOAppDefault {

import PrepareDataUtils._

val res = withFile(resourceName)(file => ZIO.attempt(getNameAndAgeOfPerson(file)))
val res =
withFile(resourceName)(file => ZIO.attempt(getNameAndAgeOfPerson(file)))

val runStream = ZStream
.fromIterableZIO(res)
.map{ person =>
.map { person =>
Person.schema.validate(person) match {
case Chunk() => Right(person)
case Chunk() => Right(person)
case Chunk(_) | Chunk(_, _) => Left(person)
}

}

val listOfValidPersons = runStream
val listOfValidPersons = runStream
.runFold((List.empty[Person], List.empty[String])) {
case ((valid, invalid), Right(person)) => (valid :+ person, invalid) // Collect valid persons
case ((valid, invalid), Left(error)) => (valid, invalid :+ error.toString) // Collect errors
case ((valid, invalid), Right(person)) =>
(valid :+ person, invalid) // Collect valid persons
case ((valid, invalid), Left(error)) =>
(valid, invalid :+ error.toString) // Collect errors
}


def run=
program

def run =
program

val program =
for {
count <- runStream.runFold(0)((accum, _) => accum + 1)
c <- listOfValidPersons
count <- runStream.runFold(0)((accum, _) => accum + 1)
c <- listOfValidPersons
_ <- Console.printLine(s"Total count: ${c._2.size}")
} yield ()

Expand Down
Loading

0 comments on commit 268141e

Please sign in to comment.