Skip to content

Commit

Permalink
Merge pull request #185 from ahjohannessen/wip-of-naming
Browse files Browse the repository at this point in the history
core: consistent naming of F constructors.
  • Loading branch information
ahjohannessen committed Oct 9, 2020
2 parents c3b6d94 + 37a3080 commit 85aae3f
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 54 deletions.
23 changes: 22 additions & 1 deletion core/src/main/scala/sec/event.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package sec

import java.time.ZonedDateTime
import java.util.UUID
import scala.util.control.NoStackTrace
import cats.Show
import cats.syntax.all._
import scodec.bits.ByteVector
Expand Down Expand Up @@ -111,7 +112,8 @@ object EventType {
sealed abstract case class SystemDefined(name: String) extends SystemType
sealed abstract case class UserDefined(name: String) extends EventType

def apply(name: String): Attempt[UserDefined] = userDefined(name)
def apply(name: String): Attempt[UserDefined] = userDefined(name)
def of[F[_]: ErrorA](name: String): F[UserDefined] = EventType(name).leftMap(InvalidEventType).liftTo[F]

///

Expand Down Expand Up @@ -163,6 +165,8 @@ object EventType {

implicit val showForEventType: Show[EventType] = Show.show[EventType](eventTypeToString)

final case class InvalidEventType(msg: String) extends RuntimeException(msg) with NoStackTrace

}

//======================================================================================================================
Expand Down Expand Up @@ -202,6 +206,23 @@ object EventData {
): Attempt[EventData] =
EventType(eventType).map(EventData(_, eventId, data, metadata, contentType))

def of[F[_]: ErrorA](
eventType: String,
eventId: UUID,
data: ByteVector,
contentType: ContentType
): F[EventData] =
EventType.of[F](eventType).map(EventData(_, eventId, data, ByteVector.empty, contentType))

def of[F[_]: ErrorA](
eventType: String,
eventId: UUID,
data: ByteVector,
metadata: ByteVector,
contentType: ContentType
): F[EventData] =
EventType.of[F](eventType).map(EventData(_, eventId, data, metadata, contentType))

///

implicit final private[sec] class EventDataOps(val ed: EventData) extends AnyVal {
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/scala/sec/id.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package sec

import cats.syntax.all._
import cats.{ApplicativeError, Eq, Show}
import cats.{Eq, Show}
import sec.utilities.{guardNonEmpty, guardNotStartsWith}

//======================================================================================================================
Expand All @@ -40,12 +40,12 @@ object StreamId {
sealed abstract case class System(name: String) extends SystemId
sealed abstract case class Normal(name: String) extends NormalId

def apply[F[_]](name: String)(implicit F: ApplicativeError[F, Throwable]): F[Id] =
from(name).leftMap(StreamIdError).liftTo[F]

def from(name: String): Attempt[Id] =
def apply(name: String): Attempt[Id] =
guardNonEmptyName(name) >>= guardNotStartsWith(metadataPrefix) >>= stringToId

def of[F[_]: ErrorA](name: String): F[Id] =
StreamId(name).leftMap(StreamIdError).liftTo[F]

final case class StreamIdError(msg: String) extends RuntimeException(msg)

///
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/sec/metadata.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ object MaxAge {
if (maxAge < 1.second) s"maxAge must be >= 1 second, it was $maxAge.".asLeft
else new MaxAge(maxAge) {}.asRight

def lift[F[_]: ErrorA](maxAge: FiniteDuration): F[MaxAge] =
def of[F[_]: ErrorA](maxAge: FiniteDuration): F[MaxAge] =
MaxAge(maxAge).orFail[F](InvalidMaxAge)

implicit val showForMaxAge: Show[MaxAge] = Show.show(_.value.toString())
Expand All @@ -139,7 +139,7 @@ object MaxCount {
if (maxCount < 1) s"max count must be >= 1, it was $maxCount.".asLeft
else new MaxCount(maxCount) {}.asRight

def lift[F[_]: ErrorA](maxCount: Int): F[MaxCount] =
def of[F[_]: ErrorA](maxCount: Int): F[MaxCount] =
MaxCount(maxCount).orFail[F](InvalidMaxCount)

implicit val showForMaxCount: Show[MaxCount] = Show.show { mc =>
Expand All @@ -160,7 +160,7 @@ object CacheControl {
if (cacheControl < 1.second) s"cache control must be >= 1, it was $cacheControl.".asLeft
else new CacheControl(cacheControl) {}.asRight

def lift[F[_]: ErrorA](cacheControl: FiniteDuration): F[CacheControl] =
def of[F[_]: ErrorA](cacheControl: FiniteDuration): F[CacheControl] =
CacheControl(cacheControl).orFail[F](InvalidCacheControl)

implicit val showForCacheControl: Show[CacheControl] = Show.show(_.value.toString())
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/sec/version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object EventNumber {
def apply(value: Long): Attempt[Exact] =
Either.cond(value >= 0L, create(value), s"value must be >= 0, but is $value")

def lift[F[_]: ErrorA](value: Long): F[Exact] =
def of[F[_]: ErrorA](value: Long): F[Exact] =
Exact(value).leftMap(InvalidExact).liftTo[F]

///
Expand Down
8 changes: 4 additions & 4 deletions fs2/src/main/scala-2.13/sec/syntax/metastreams.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class MetaStreamsOps[F[_]: ErrorM](val ms: MetaStreams[F]) {
setMaxAgeF(id, expectedRevision, age, uc.some)

private def setMaxAgeF(id: Id, er: StreamRevision, age: FiniteDuration, uc: Option[UserCredentials]): F[WriteResult] =
MaxAge.lift[F](age) >>= (ms.setMaxAge(id, er, _, uc))
MaxAge.of[F](age) >>= (ms.setMaxAge(id, er, _, uc))

def unsetMaxAge(id: Id, expectedRevision: StreamRevision): F[WriteResult] =
ms.unsetMaxAge(id, expectedRevision, None)
Expand All @@ -61,7 +61,7 @@ final class MetaStreamsOps[F[_]: ErrorM](val ms: MetaStreams[F]) {
setMaxCountF(id, expectedRevision, count, uc.some)

private def setMaxCountF(id: Id, er: StreamRevision, count: Int, uc: Option[UserCredentials]): F[WriteResult] =
MaxCount.lift[F](count) >>= (ms.setMaxCount(id, er, _, uc))
MaxCount.of[F](count) >>= (ms.setMaxCount(id, er, _, uc))

def unsetMaxCount(id: Id, expectedRevision: StreamRevision): F[WriteResult] =
ms.unsetMaxCount(id, expectedRevision, None)
Expand All @@ -86,7 +86,7 @@ final class MetaStreamsOps[F[_]: ErrorM](val ms: MetaStreams[F]) {
cc: FiniteDuration,
uc: Option[UserCredentials]
): F[WriteResult] =
CacheControl.lift[F](cc) >>= (ms.setCacheControl(id, er, _, uc))
CacheControl.of[F](cc) >>= (ms.setCacheControl(id, er, _, uc))

def unsetCacheControl(id: Id, expectedRevision: StreamRevision): F[WriteResult] =
ms.unsetCacheControl(id, expectedRevision, None)
Expand Down Expand Up @@ -115,7 +115,7 @@ final class MetaStreamsOps[F[_]: ErrorM](val ms: MetaStreams[F]) {
setTruncateBeforeF(id, expectedRevision, truncateBefore, uc.some)

private def setTruncateBeforeF(id: Id, er: StreamRevision, tb: Long, uc: Option[UserCredentials]): F[WriteResult] =
EventNumber.Exact.lift[F](tb) >>= (ms.setTruncateBefore(id, er, _, uc))
EventNumber.Exact.of[F](tb) >>= (ms.setTruncateBefore(id, er, _, uc))

def unsetTruncateBefore(id: Id, expectedRevision: StreamRevision): F[WriteResult] =
ms.unsetTruncateBefore(id, expectedRevision, None)
Expand Down
8 changes: 4 additions & 4 deletions fs2/src/main/scala-3/sec/syntax/metastreams.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ trait MetaStreamsSyntax {
setMaxAgeF(id, expectedRevision, age, uc.some)

private def setMaxAgeF(id: Id, er: StreamRevision, m: FiniteDuration, uc: Option[UserCredentials]): F[WriteResult] =
MaxAge.lift[F](m) >>= (ms.setMaxAge(id, er, _, uc))
MaxAge.of[F](m) >>= (ms.setMaxAge(id, er, _, uc))

def unsetMaxAge(id: Id, expectedRevision: StreamRevision): F[WriteResult] =
ms.unsetMaxAge(id, expectedRevision, None)
Expand All @@ -55,7 +55,7 @@ trait MetaStreamsSyntax {
setMaxCountF(id, expectedRevision, count, uc.some)

private def setMaxCountF(id: Id, er: StreamRevision, count: Int, uc: Option[UserCredentials]): F[WriteResult] =
MaxCount.lift[F](count) >>= (ms.setMaxCount(id, er, _, uc))
MaxCount.of[F](count) >>= (ms.setMaxCount(id, er, _, uc))

def unsetMaxCount(id: Id, expectedRevision: StreamRevision): F[WriteResult] =
ms.unsetMaxCount(id, expectedRevision, None)
Expand All @@ -70,7 +70,7 @@ trait MetaStreamsSyntax {
setCacheControlF(id, expectedRevision, cacheControl, uc.some)

private def setCacheControlF(id: Id, er: StreamRevision, cacheControl: FiniteDuration, uc: Option[UserCredentials]): F[WriteResult] =
CacheControl.lift[F](cacheControl) >>= (ms.setCacheControl(id, er, _, uc))
CacheControl.of[F](cacheControl) >>= (ms.setCacheControl(id, er, _, uc))

def unsetCacheControl(id: Id, expectedRevision: StreamRevision): F[WriteResult] =
ms.unsetCacheControl(id, expectedRevision, None)
Expand All @@ -94,7 +94,7 @@ trait MetaStreamsSyntax {
setTruncateBeforeF(id, expectedRevision, truncateBefore, uc.some)

private def setTruncateBeforeF(id: Id, er: StreamRevision, tb: Long, uc: Option[UserCredentials]): F[WriteResult] =
EventNumber.Exact.lift[F](tb) >>= (ms.setTruncateBefore(id, er, _, uc))
EventNumber.Exact.of[F](tb) >>= (ms.setTruncateBefore(id, er, _, uc))

def unsetTruncateBefore(id: Id, expectedRevision: StreamRevision): F[WriteResult] =
ms.unsetTruncateBefore(id, expectedRevision, None)
Expand Down
18 changes: 9 additions & 9 deletions tests/src/test/scala/sec/api/mapping/streams.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class StreamsMappingSpec extends mutable.Specification {
"mkSubscribeToStreamReq" >> {
import EventNumber._

val sid = StreamId.from("abc").unsafe
val sid = StreamId("abc").unsafe

def test(exclusiveFrom: Option[EventNumber], resolveLinkTos: Boolean) =
mkSubscribeToStreamReq(sid, exclusiveFrom, resolveLinkTos) shouldEqual s
Expand Down Expand Up @@ -198,7 +198,7 @@ class StreamsMappingSpec extends mutable.Specification {

"mkReadStreamReq" >> {

val sid = sec.StreamId.from("abc").unsafe
val sid = sec.StreamId("abc").unsafe

def test(rd: Direction, from: EventNumber, count: Long, rlt: Boolean) =
mkReadStreamReq(sid, from, rd, count, rlt) shouldEqual s
Expand Down Expand Up @@ -250,7 +250,7 @@ class StreamsMappingSpec extends mutable.Specification {
"mkDeleteReq" >> {

import s.DeleteReq.Options.ExpectedStreamRevision
val sid = sec.StreamId.from("abc").unsafe
val sid = sec.StreamId("abc").unsafe

def test(sr: StreamRevision, esr: ExpectedStreamRevision) =
mkDeleteReq(sid, sr) shouldEqual
Expand All @@ -265,7 +265,7 @@ class StreamsMappingSpec extends mutable.Specification {
"mkTombstoneReq" >> {

import s.TombstoneReq.Options.ExpectedStreamRevision
val sid = sec.StreamId.from("abc").unsafe
val sid = sec.StreamId("abc").unsafe

def test(sr: StreamRevision, esr: ExpectedStreamRevision) =
mkTombstoneReq(sid, sr) shouldEqual
Expand All @@ -280,7 +280,7 @@ class StreamsMappingSpec extends mutable.Specification {
"mkAppendHeaderReq" >> {

import s.AppendReq.Options.ExpectedStreamRevision
val sid = sec.StreamId.from("abc").unsafe
val sid = sec.StreamId("abc").unsafe

def test(sr: StreamRevision, esr: ExpectedStreamRevision) =
mkAppendHeaderReq(sid, sr) shouldEqual
Expand Down Expand Up @@ -391,15 +391,15 @@ class StreamsMappingSpec extends mutable.Specification {
.withMetadata(linkMetadata)

val eventRecord = EventRecord(
sec.StreamId.from(streamId).unsafe,
sec.StreamId(streamId).unsafe,
sec.EventNumber.exact(revision),
sec.Position.exact(commit, prepare),
sec.EventData(sec.EventType(eventType).unsafe, JUUID.fromString(id), data, customMeta, sec.ContentType.Json),
created
)

val linkRecord = sec.EventRecord(
sec.StreamId.from(linkStreamId).unsafe,
sec.StreamId(linkStreamId).unsafe,
sec.EventNumber.exact(linkRevision),
sec.Position.exact(linkCommit, linkPrepare),
sec.EventData(sec.EventType.LinkTo, JUUID.fromString(linkId), linkData, linkCustomMeta, sec.ContentType.Binary),
Expand Down Expand Up @@ -459,7 +459,7 @@ class StreamsMappingSpec extends mutable.Specification {
.withMetadata(metadata)

val eventRecord = sec.EventRecord(
sec.StreamId.from(streamId).unsafe,
sec.StreamId(streamId).unsafe,
sec.EventNumber.exact(revision),
sec.Position.exact(commit, prepare),
sec.EventData(sec.EventType(eventType).unsafe, JUUID.fromString(id), data, customMeta, sec.ContentType.Binary),
Expand Down Expand Up @@ -592,7 +592,7 @@ class StreamsMappingSpec extends mutable.Specification {
import s.AppendResp.WrongExpectedVersion.ExpectedRevisionOption
import s.AppendResp.WrongExpectedVersion.CurrentRevisionOption

val sid: StreamId.Id = sec.StreamId.from("abc").unsafe
val sid: StreamId.Id = sec.StreamId("abc").unsafe
val test: s.AppendResp => ErrorOr[WriteResult] = mkWriteResult[ErrorOr](sid, _)

val successRevOne = Success().withCurrentRevision(1L).withPosition(s.AppendResp.Position(1L, 1L))
Expand Down
10 changes: 9 additions & 1 deletion tests/src/test/scala/sec/event.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package sec
import java.{util => ju}
import java.time.ZonedDateTime
import java.util.UUID

import cats.syntax.all._
import scodec.bits.ByteVector
import org.specs2.mutable.Specification
import sec.EventType.InvalidEventType
import sec.arbitraries._
import sec.helpers.text.encodeToBV

Expand All @@ -33,7 +35,7 @@ class EventSpec extends Specification {
ByteVector.encodeUtf8(data).leftMap(_.getMessage).unsafe

val er: EventRecord = sec.EventRecord(
StreamId.from("abc-1234").unsafe,
StreamId("abc-1234").unsafe,
EventNumber.exact(5L),
sampleOf[Position.Exact],
EventData("et", sampleOf[ju.UUID], bv("abc"), ContentType.Binary).unsafe,
Expand Down Expand Up @@ -132,6 +134,12 @@ class EventTypeSpec extends Specification {
EventType("users") should beRight(EventType.userDefined("users").unsafe)
}

"of" >> {
EventType.of[ErrorOr]("") should beLeft(InvalidEventType("Event type name cannot be empty"))
EventType.of[ErrorOr]("$users") should beLeft(InvalidEventType("value must not start with $, but is $users"))
EventType.of[ErrorOr]("users") should beRight(EventType.userDefined("users").unsafe)
}

"eventTypeToString" >> {
EventType.eventTypeToString(EventType.StreamDeleted) shouldEqual st.StreamDeleted
EventType.eventTypeToString(EventType.StatsCollected) shouldEqual st.StatsCollected
Expand Down
46 changes: 23 additions & 23 deletions tests/src/test/scala/sec/id.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,38 @@ class StreamIdSpec extends Specification with Discipline {

import StreamId.{systemStreams => ss}

val normalId = StreamId.normal("normal").unsafe
val systemId = StreamId.system("system").unsafe

"from" >> {
StreamId.from("") should beLeft("name cannot be empty")
StreamId.from("$$meta") should beLeft("value must not start with $$, but is $$meta")
StreamId.from("$users") shouldEqual StreamId.system("users")
StreamId.from("users") shouldEqual StreamId.normal("users")
StreamId.from(ss.All) shouldEqual StreamId.All.asRight
StreamId.from(ss.Settings) shouldEqual StreamId.Settings.asRight
StreamId.from(ss.Stats) shouldEqual StreamId.Stats.asRight
StreamId.from(ss.Scavenges) shouldEqual StreamId.Scavenges.asRight
StreamId.from(ss.Streams) shouldEqual StreamId.Streams.asRight
}
val normalId: StreamId.Normal = StreamId.normal("normal").unsafe
val systemId: StreamId.System = StreamId.system("system").unsafe

"apply" >> {
StreamId("") should beLeft("name cannot be empty")
StreamId("$$meta") should beLeft("value must not start with $$, but is $$meta")
StreamId("$users") shouldEqual StreamId.system("users")
StreamId("users") shouldEqual StreamId.normal("users")
StreamId(ss.All) shouldEqual StreamId.All.asRight
StreamId(ss.Settings) shouldEqual StreamId.Settings.asRight
StreamId(ss.Stats) shouldEqual StreamId.Stats.asRight
StreamId(ss.Scavenges) shouldEqual StreamId.Scavenges.asRight
StreamId(ss.Streams) shouldEqual StreamId.Streams.asRight
}

"of" >> {

StreamId[ErrorOr]("") should beLike { case Left(StreamId.StreamIdError("name cannot be empty")) =>
StreamId.of[ErrorOr]("") should beLike { case Left(StreamId.StreamIdError("name cannot be empty")) =>
ok
}

StreamId[ErrorOr]("$$m") should beLike {
StreamId.of[ErrorOr]("$$m") should beLike {
case Left(StreamId.StreamIdError("value must not start with $$, but is $$m")) => ok
}

StreamId[ErrorOr]("$users") shouldEqual StreamId.system("users")
StreamId[ErrorOr]("users") shouldEqual StreamId.normal("users")
StreamId[ErrorOr](ss.All) shouldEqual StreamId.All.asRight
StreamId[ErrorOr](ss.Settings) shouldEqual StreamId.Settings.asRight
StreamId[ErrorOr](ss.Stats) shouldEqual StreamId.Stats.asRight
StreamId[ErrorOr](ss.Scavenges) shouldEqual StreamId.Scavenges.asRight
StreamId[ErrorOr](ss.Streams) shouldEqual StreamId.Streams.asRight
StreamId.of[ErrorOr]("$users") shouldEqual StreamId.system("users")
StreamId.of[ErrorOr]("users") shouldEqual StreamId.normal("users")
StreamId.of[ErrorOr](ss.All) shouldEqual StreamId.All.asRight
StreamId.of[ErrorOr](ss.Settings) shouldEqual StreamId.Settings.asRight
StreamId.of[ErrorOr](ss.Stats) shouldEqual StreamId.Stats.asRight
StreamId.of[ErrorOr](ss.Scavenges) shouldEqual StreamId.Scavenges.asRight
StreamId.of[ErrorOr](ss.Streams) shouldEqual StreamId.Streams.asRight
}

"streamIdToString" >> {
Expand Down
6 changes: 3 additions & 3 deletions tests/src/test/scala/sec/version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class VersionSpec extends Specification with Discipline {
Exact(0L) should beRight(EventNumber.exact(0L))
}

"Exact.lift" >> {
Exact.lift[ErrorOr](-1L) should beLeft(InvalidExact("value must be >= 0, but is -1"))
Exact.lift[ErrorOr](0L) should beRight(EventNumber.exact(0L))
"Exact.of" >> {
Exact.of[ErrorOr](-1L) should beLeft(InvalidExact("value must be >= 0, but is -1"))
Exact.of[ErrorOr](0L) should beRight(EventNumber.exact(0L))
}

"Show" >> {
Expand Down

0 comments on commit 85aae3f

Please sign in to comment.