Skip to content

Commit

Permalink
Merge pull request #9 from bilal-fazlani/unify-package-name
Browse files Browse the repository at this point in the history
unify package name by removing .protocol package
  • Loading branch information
bilal-fazlani authored Jul 13, 2023
2 parents 98dba3e + dc4a2ab commit 9e965ce
Show file tree
Hide file tree
Showing 32 changed files with 25 additions and 44 deletions.
3 changes: 1 addition & 2 deletions docs/api-reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ hide:
# API Reference

!!! tip
All the functionality of zio-maelstrom is available via two import statements
All the functionality of zio-maelstrom is available via following import statement

```scala
import com.bilalfazlani.zioMaelstrom.*
import com.bilalfazlani.zioMaelstrom.protocol.*
```

## Primitive Types
Expand Down
1 change: 0 additions & 1 deletion examples/broadcast/src/main/scala/com/example/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package com.example.broadcast
import zio.json.*
import zio.*
import com.bilalfazlani.zioMaelstrom.*
import com.bilalfazlani.zioMaelstrom.protocol.*
// }

// input_messages {
Expand Down
1 change: 0 additions & 1 deletion examples/echo/src/main/scala/com/example/Calculator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.example.calculator

import zio.json.*
import zio.*
import com.bilalfazlani.zioMaelstrom.protocol.*
import com.bilalfazlani.zioMaelstrom.*

// format: off
Expand Down
1 change: 0 additions & 1 deletion examples/echo/src/main/scala/com/example/ErrorDocs.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example

import com.bilalfazlani.zioMaelstrom.*
import com.bilalfazlani.zioMaelstrom.protocol.*
import zio.json.*
import zio.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.example

import zio.*
import com.bilalfazlani.zioMaelstrom.*
import com.bilalfazlani.zioMaelstrom.protocol.*
import zio.json.*

object Main extends ZIOAppDefault {
Expand Down
1 change: 0 additions & 1 deletion examples/echo/src/main/scala/com/example/IODocs.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example

import com.bilalfazlani.zioMaelstrom.*
import com.bilalfazlani.zioMaelstrom.protocol.*
import zio.json.JsonCodec
import zio.*

Expand Down
1 change: 0 additions & 1 deletion examples/echo/src/main/scala/com/example/KvStoreDocs.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.kvstore

import com.bilalfazlani.zioMaelstrom.*
import com.bilalfazlani.zioMaelstrom.protocol.*
import zio.*

object SeqKvExample {
Expand Down
1 change: 0 additions & 1 deletion examples/echo/src/main/scala/com/example/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.example.echo
//imports {
import zio.json.{JsonEncoder, JsonDecoder}
import zio.{ZIOAppDefault, ZIO}
import com.bilalfazlani.zioMaelstrom.protocol.*
import com.bilalfazlani.zioMaelstrom.*
//}

Expand Down
1 change: 0 additions & 1 deletion examples/echo/src/main/scala/com/example/PingPong.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.example.pingpong

import zio.json.*
import zio.*
import com.bilalfazlani.zioMaelstrom.protocol.*
import com.bilalfazlani.zioMaelstrom.*

case class Ping(msg_id: MessageId, `type`: String = "ping") extends Sendable, NeedsReply
Expand Down
1 change: 0 additions & 1 deletion examples/echo/src/main/scala/com/example/TsoDocs.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.tso

import com.bilalfazlani.zioMaelstrom.*
import com.bilalfazlani.zioMaelstrom.protocol.*
import zio.*

object TsoExample {
Expand Down
1 change: 0 additions & 1 deletion examples/unique-ids/src/main/scala/com/example/Main.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.uniqueIds

//imports {
import com.bilalfazlani.zioMaelstrom.protocol.*
import com.bilalfazlani.zioMaelstrom.*
import zio.json.{JsonDecoder, JsonEncoder}
import zio.{ZIOAppDefault, ZIO, Ref, ZLayer}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bilalfazlani.zioMaelstrom

import zio.*
import protocol.*
import zio.concurrent.ConcurrentMap

private case class TimedPromise(promise: Promise[AskError, GenericMessage], startTime: Long) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.bilalfazlani.zioMaelstrom

import protocol.*

case class Context(me: NodeId, others: Set[NodeId])

object Context:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bilalfazlani.zioMaelstrom

import java.nio.file.Path
import protocol.*
import zio.json.*
import zio.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.bilalfazlani.zioMaelstrom
package protocol

import zio.json.ast.Json
import zio.json.JsonDecoder
Expand All @@ -12,9 +11,13 @@ private[zioMaelstrom] case class GenericDetails(

extension (parent: Json)
private def getChildOptional[A: JsonDecoder](field: String): Either[String, Option[A]] =
parent.asObject.flatMap(_.get(field)).fold(Right(None))(json => JsonDecoder[A].fromJsonAST(json).map(Some(_)))
parent.asObject
.flatMap(_.get(field))
.fold(Right(None))(json => JsonDecoder[A].fromJsonAST(json).map(Some(_)))
private def getChild[A: JsonDecoder](field: String): Either[String, A] =
parent.asObject.flatMap(_.get(field)).fold(Left(s"missing field '$field'"))(json => JsonDecoder[A].fromJsonAST(json))
parent.asObject
.flatMap(_.get(field))
.fold(Left(s"missing field '$field'"))(json => JsonDecoder[A].fromJsonAST(json))

private[zioMaelstrom] object GenericDetails {
val empty = GenericDetails(None, None, None)
Expand Down Expand Up @@ -60,12 +63,22 @@ private[zioMaelstrom] case class GenericMessage(
private[zioMaelstrom] object GenericMessage {
given JsonDecoder[GenericMessage] = JsonDecoder[Json].mapOrFail[GenericMessage](ast =>
for {
obj <- ast.asObject.toRight("message is not a json object")
src <- obj.getChild[NodeId]("src")
dest <- obj.getChild[NodeId]("dest")
body <- obj.getChildOptional[Json]("body")
details <- body.fold(Right(GenericDetails.empty))(body => JsonDecoder[GenericDetails].fromJsonAST(body))
} yield GenericMessage(src, dest, details.messageType, details.messageId, details.inReplyTo, body, ast)
obj <- ast.asObject.toRight("message is not a json object")
src <- obj.getChild[NodeId]("src")
dest <- obj.getChild[NodeId]("dest")
body <- obj.getChildOptional[Json]("body")
details <- body.fold(Right(GenericDetails.empty))(body =>
JsonDecoder[GenericDetails].fromJsonAST(body)
)
} yield GenericMessage(
src,
dest,
details.messageType,
details.messageId,
details.inReplyTo,
body,
ast
)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bilalfazlani.zioMaelstrom

import protocol.*
import zio.*
import zio.stream.{ZStream, ZSink}
import zio.json.JsonDecoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.bilalfazlani.zioMaelstrom

import zio.{Scope, ZIO, ZLayer}
import zio.json.JsonDecoder
import protocol.*

private[zioMaelstrom] trait InputChannel:
def partitionInputs: ZIO[Scope, Nothing, Inputs]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bilalfazlani.zioMaelstrom

import zio.{ZIO, Ref, ZLayer}
import protocol.MessageId

trait MessageIdStore:
def next: ZIO[Any, Nothing, MessageId]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bilalfazlani.zioMaelstrom

import protocol.*
import zio.*
import zio.json.{JsonEncoder, JsonDecoder}

Expand Down Expand Up @@ -54,8 +53,8 @@ private case class MessageSenderLive(
to: NodeId,
timeout: Duration
): IO[AskError, Res] = for {
_ <- send(body, to)
_ <- logger.debug(s"waiting for reply from ${to} for message id ${body.msg_id}...")
_ <- send(body, to)
_ <- logger.debug(s"waiting for reply from ${to} for message id ${body.msg_id}...")
genericMessage <- ZIO.scoped(callbackRegistry.awaitCallback(body.msg_id, to, timeout))
decoded <-
if genericMessage.isError then {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bilalfazlani.zioMaelstrom

import zio.*
import protocol.*
import zio.json.{JsonEncoder, EncoderOps}

private[zioMaelstrom] trait OutputChannel:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.bilalfazlani.zioMaelstrom
package protocol

import zio.json.*
import scala.annotation.targetName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bilalfazlani.zioMaelstrom

import protocol.*
import zio.*
import zio.json.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.bilalfazlani.zioMaelstrom

import zio.*
import zio.json.*
import protocol.*

private[zioMaelstrom] trait KvService:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.bilalfazlani.zioMaelstrom

import zio.*
import zio.json.*
import protocol.*

private[zioMaelstrom] case class KvRead[Key](key: Key, msg_id: MessageId, `type`: String = "read")
extends NeedsReply,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bilalfazlani.zioMaelstrom

import protocol.*
import zio.*
import zio.json.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bilalfazlani.zioMaelstrom

import protocol.*
import zio.*
import zio.json.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bilalfazlani.zioMaelstrom

import protocol.*
import zio.*
import zio.json.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bilalfazlani.zioMaelstrom

import protocol.*
import zio.*
import zio.json.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.bilalfazlani.zioMaelstrom

import zio.test.*
import zio.*
import com.bilalfazlani.zioMaelstrom.protocol.*
import zio.json.*
import testkit.TestRuntime
import testkit.TestRuntime.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import zio.*
import zio.json.*
import testkit.TestRuntime
import testkit.TestRuntime.*
import com.bilalfazlani.zioMaelstrom.protocol.*

object RequestHandlerTest extends ZIOSpecDefault {
def isCI = sys.env.get("CI").contains("true")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testkit

import zio.*
import zio.json.*
import protocol.*

case class KvFake(ref: Ref.Synchronized[Map[Any, Any]], messageIdStore: MessageIdStore)
extends KvService:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.bilalfazlani.zioMaelstrom
package testkit

import zio.*
import protocol.*
import zio.json.{JsonEncoder, EncoderOps}

type TestRuntime = MaelstromRuntime & Queue[Message[Sendable]] & Queue[String] & CallbackRegistry
Expand Down

0 comments on commit 9e965ce

Please sign in to comment.