Last active
May 25, 2024 10:20
-
-
Save dacr/f8f284a27ea4f0e24e357a7ad5de3c3c to your computer and use it in GitHub Desktop.
ZIO learning - quick layer definition / published by https://github.com/dacr/code-examples-manager #0abcfad6-eb23-4b36-86c9-9d5efcbcd5e7/3523621554a9b684858fa69da2f5e9c29fe7e49e
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
{"timestamp": "2022-03-04T11:25:50Z", "value": 41.0} | |
{"timestamp": "2022-03-04T11:26:17Z", "value": 42.0} | |
{"timestamp": "2022-03-04T11:26:42Z", "value": 42.0} |
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
// summary : ZIO learning - quick layer definition | |
// keywords : scala, zio, layers, to-layer, nio, json, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 0abcfad6-eb23-4b36-86c9-9d5efcbcd5e7 | |
// attachments : zio-learning-31-layers-1.json | |
// created-on : 2022-03-04T11:20:53+01:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "dev.zio::zio-nio:2.0.1" | |
//> using dep "dev.zio::zio-json:0.5.0" | |
// --------------------- | |
/* | |
Inspired from https://www.youtube.com/watch?v=PJTn33Qj1nc&ab_channel=Ziverge | |
*/ | |
import zio.* | |
import zio.json.* | |
import zio.nio.file.* | |
import java.time.Instant | |
case class Sample(timestamp: Instant, value: Double) | |
object Sample: | |
implicit val codec: JsonCodec[Sample] = DeriveJsonCodec.gen | |
case class Samples(temperatureSamples: Seq[Sample]) | |
object Encapsulated extends ZIOAppDefault: | |
val samples = ZLayer( | |
for | |
lines <- Files.readAllLines(Path("zio-learning-31-layers-1.json")) | |
samples <- ZIO.foreach(lines)(line => ZIO.fromEither(line.fromJson[Sample])) | |
yield Samples(samples) | |
) | |
val logic = | |
for | |
samples <- ZIO.serviceWith[Samples](_.temperatureSamples) | |
_ <- ZIO.foreach(samples)(t => Console.printLine(t.toJson)) | |
yield () | |
override def run = logic.provideSomeLayer(samples) | |
Encapsulated.main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment