https://github.com/nikdon/scala-sample
Random object generation with shapeless
https://github.com/nikdon/scala-sample
generic-programming scala shapeless test-data
Last synced: 11 months ago
JSON representation
Random object generation with shapeless
- Host: GitHub
- URL: https://github.com/nikdon/scala-sample
- Owner: nikdon
- Created: 2016-12-17T19:02:16.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-14T10:26:46.000Z (almost 9 years ago)
- Last Synced: 2025-01-16T00:29:05.414Z (about 1 year ago)
- Topics: generic-programming, scala, shapeless, test-data
- Language: Scala
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# scala-sample
[](https://travis-ci.org/nikdon/scala-sample)
[](https://codecov.io/gh/nikdon/scala-sample)
[](https://jitpack.io/#nikdon/scala-sample)
**scala-sample** is code snippet for random object generation from [**shapeless guide**](https://github.com/underscoreio/shapeless-guide)
as a library that can be used in any scala project of version 2.11.+ (version of scala-sample "1.0.0") and 2.12.+ (see below)
## Table of contents
1. [Quick start](#quick-start)
2. [Usage](#usage)
3. [License](#license)
## Quick start
To use **scala-sample** add following to build file:
```scala
resolvers += "jitpack" at "https://jitpack.io"
libraryDependencies += "com.github.nikdon" % "scala-sample" % "2.0.0"
```
## Usage
```scala
import com.github.nikdon.sample.Sample
import java.time.Instant
sealed trait Foo
case class Fiz(fiz: String) extends Foo
case class Buz(buz: String) extends Foo
case class SomeEvent(happenedAt: Instant)
object Test extends App {
val foo = Sample[Foo].get // returns an instance of type Foo (Fiz or Buz)
val nFoo = Sample[Foo].getN(2) // returns a List[Foo] of size 2
// in some cases it is necessary to provide an implementation of `Sample`
implicit val instantSample = new Sample[Instant] {
override def get: Instant = Instant.now
}
val event = Sample[SomeEvent].get // returns a SomeEvent
// it works with Option[_] and Either[_, _]
val someEvent = Sample[Option[SomeEvent]].get
val eitherStringOrEvent = Sample[Either[String, SomeEvent]].get
println(s"foo = $foo")
println(s"nFoo = $nFoo")
println(s"event = $event")
println(s"someEvent = $someEvent")
println(s"eitherStringOrEvent = $eitherStringOrEvent")
}
```
## License
Please refer to [**shapeless guide**](https://github.com/underscoreio/shapeless-guide)'s license.
[](https://github.com/igrigorik/ga-beacon)