From 91032192f02af479662e50ee06e78bde873c6c1b Mon Sep 17 00:00:00 2001 From: David Barri Date: Tue, 6 Sep 2022 15:14:28 +1000 Subject: [PATCH] Add `Gen.genIP4` --- doc/changelog/1.1.0.md | 2 ++ gen/shared/src/main/scala/nyaya/gen/Gen.scala | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/doc/changelog/1.1.0.md b/doc/changelog/1.1.0.md index ed4ccee..4976bef 100644 --- a/doc/changelog/1.1.0.md +++ b/doc/changelog/1.1.0.md @@ -11,5 +11,7 @@ GenJson(4).sample() ``` +* Added `Gen.ip4: Gen[java.net.InetAddress]` for generating random IP4 addresses + * Update Scala.js to 1.10 * Update Scala 3 to 3.1 diff --git a/gen/shared/src/main/scala/nyaya/gen/Gen.scala b/gen/shared/src/main/scala/nyaya/gen/Gen.scala index f201620..6948fb2 100644 --- a/gen/shared/src/main/scala/nyaya/gen/Gen.scala +++ b/gen/shared/src/main/scala/nyaya/gen/Gen.scala @@ -2,6 +2,7 @@ package nyaya.gen import cats.data._ import cats.{Distributive, Eval, Functor, Monad, Traverse, ~>} +import java.net.InetAddress import java.time.ZoneId import java.util.UUID import scala.annotation.{switch, tailrec} @@ -879,6 +880,13 @@ object Gen { def dateTime(implicit genNow: Gen[Now]): DateTimeBuilder = DateTimeBuilder.default(genNow) + // ------------------ + // Networking related + // ------------------ + + lazy val ip4: Gen[InetAddress] = + Gen.byte.arraySeq(4).map(a => InetAddress.getByAddress(a.toArray)) + // -------------------------------------------------------------- // Traverse using plain Scala collections and CanBuildFrom (fast) // --------------------------------------------------------------