Skip to content

Commit 10999b4

Browse files
authored
feat: circe module (#24)
* feat: circe module * docs: information about how to use circe module
1 parent 90cf61f commit 10999b4

File tree

20 files changed

+105
-134
lines changed

20 files changed

+105
-134
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# Scala Hashes [![Build](https://github.com/avast/scala-hashes/actions/workflows/build.yml/badge.svg)](https://github.com/avast/scala-hashes/actions/workflows/build.yml) [![Version](https://badgen.net/maven/v/maven-central/com.avast.hashes/scala-hashes_2.13)](https://repo1.maven.org/maven2/com/avast/hashes/scala-hashes_2.13/)
22

3-
Case-classes representing MD5, SHA1 and SHA256. It also contains converters/encoders for [circe](https://github.com/travisbrown/circe) and [json4s](https://github.com/json4s/json4s) JSON libraries. Dependencies on this libraries are just optional (you must add the dependency in your project).
3+
Case-classes representing MD5, SHA1, and SHA256 hashes.
44

55
```gradle
66
compile "com.avast.hashes:scala-hashes_2.13:$versionHere"
77
```
8+
9+
There is also a support for [circe](https://github.com/travisbrown/circe) JSON library:
10+
```gradle
11+
compile "com.avast.hashes:scala-hashes-circe_2.13:$versionHere"
12+
```
13+
Then you can import encoders and decoders using `import com.avast.scala.hashes.circe._`

build.gradle

+65-72
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,87 @@
1-
buildscript {
2-
repositories {
3-
jcenter()
4-
dependencies {
5-
classpath 'com.github.ben-manes:gradle-versions-plugin:0.39.0'
6-
}
7-
}
8-
}
9-
101
plugins {
112
id 'signing'
12-
id('io.github.gradle-nexus.publish-plugin') version '1.0.0'
3+
id('io.github.gradle-nexus.publish-plugin') version '1.1.0'
134
}
145

15-
apply plugin: 'idea'
16-
apply plugin: 'scala'
17-
apply plugin: 'java-library'
18-
apply plugin: 'maven-publish'
19-
apply plugin: 'com.github.ben-manes.versions'
20-
21-
sourceCompatibility = JavaVersion.VERSION_1_8
6+
allprojects {
7+
group 'com.avast.hashes'
8+
version = version == 'unspecified' ? 'DEVELOPER-SNAPSHOT' : version
9+
}
2210

2311
ext {
2412
circeVersion = '0.13.0'
2513
}
2614

27-
group = 'com.avast.hashes'
28-
archivesBaseName = "scala-hashes_$scalaVersion"
29-
30-
repositories {
31-
mavenCentral()
15+
nexusPublishing {
16+
repositories {
17+
sonatype()
18+
}
3219
}
3320

34-
dependencies {
35-
api "org.scala-lang:scala-library:${scalaLibraryVersion}"
36-
compileOnly "org.json4s:json4s-core_${scalaVersion}:3.6.9"
37-
compileOnly "io.circe:circe-core_${scalaVersion}:${circeVersion}"
21+
subprojects {
22+
apply plugin: 'scala'
23+
apply plugin: 'java-library'
24+
apply plugin: 'maven-publish'
3825

39-
testImplementation "org.scalatest:scalatest_${scalaVersion}:3.2.0"
40-
testImplementation "org.scalatestplus:junit-4-12_${scalaVersion}:3.1.3.0"
41-
testImplementation "io.circe:circe-core_${scalaVersion}:${circeVersion}"
42-
testImplementation "junit:junit:4.13.2"
43-
}
26+
sourceCompatibility = JavaVersion.VERSION_1_8
4427

45-
java {
46-
withJavadocJar()
47-
withSourcesJar()
48-
}
28+
repositories {
29+
mavenCentral()
30+
}
4931

50-
publishing {
51-
publications {
52-
mavenJava(MavenPublication) {
53-
from(components.java)
54-
artifactId = archivesBaseName
55-
pom {
56-
name = 'Scala Hashes'
57-
description = 'Case-classes representing MD5, SHA1 and SHA256.'
58-
url = 'https://github.com/avast/scala-hashes'
59-
licenses {
60-
license {
61-
name = 'The MIT License'
62-
url = 'http://www.opensource.org/licenses/mit-license.php'
63-
}
64-
}
65-
developers {
66-
developer {
67-
id = 'augi'
68-
name = 'Michal Augustýn'
69-
32+
dependencies {
33+
api "org.scala-lang:scala-library:${scalaLibraryVersion}"
34+
35+
testImplementation "org.scalatest:scalatest_${scalaVersion}:3.2.0"
36+
testImplementation "org.scalatestplus:junit-4-12_${scalaVersion}:3.1.3.0"
37+
testImplementation "junit:junit:4.13.2"
38+
}
39+
40+
java {
41+
withJavadocJar()
42+
withSourcesJar()
43+
}
44+
45+
afterEvaluate {
46+
47+
publishing {
48+
publications {
49+
mavenJava(MavenPublication) {
50+
from(components.java)
51+
artifactId = archivesBaseName
52+
pom {
53+
name = 'Scala Hashes'
54+
description = 'Case-classes representing MD5, SHA1 and SHA256.'
55+
url = 'https://github.com/avast/scala-hashes'
56+
licenses {
57+
license {
58+
name = 'The MIT License'
59+
url = 'http://www.opensource.org/licenses/mit-license.php'
60+
}
61+
}
62+
developers {
63+
developer {
64+
id = 'augi'
65+
name = 'Michal Augustýn'
66+
67+
}
68+
}
69+
scm {
70+
connection = 'scm:git:git://github.com/avast/scala-hashes.git'
71+
developerConnection = 'scm:git:ssh://github.com/avast/scala-hashes.git'
72+
url = 'https://github.com/avast/scala-hashes'
73+
}
7074
}
7175
}
72-
scm {
73-
connection = 'scm:git:git://github.com/avast/scala-hashes.git'
74-
developerConnection = 'scm:git:ssh://github.com/avast/scala-hashes.git'
75-
url = 'https://github.com/avast/scala-hashes'
76-
}
7776
}
7877
}
7978
}
80-
}
81-
82-
signing {
83-
String base64Key = System.getenv('SIGNING_KEY')
84-
if (base64Key) {
85-
useInMemoryPgpKeys(new String(Base64.decoder.decode(base64Key)), System.getenv('SIGNING_PASSWORD'))
86-
sign publishing.publications
87-
}
88-
}
8979

90-
nexusPublishing {
91-
repositories {
92-
sonatype()
80+
signing {
81+
String base64Key = System.getenv('SIGNING_KEY')
82+
if (base64Key) {
83+
useInMemoryPgpKeys(new String(Base64.decoder.decode(base64Key)), System.getenv('SIGNING_PASSWORD'))
84+
sign publishing.publications
85+
}
9386
}
9487
}

circe/build.gradle

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
archivesBaseName = "scala-hashes-circe_$scalaVersion"
2+
3+
dependencies {
4+
api project(':core')
5+
api "io.circe:circe-core_${scalaVersion}:${circeVersion}"
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.avast.scala.hashes
2+
3+
import cats.syntax.all._
4+
import io.circe._
5+
6+
package object circe {
7+
implicit val Sha256Decoder: Decoder[Sha256] = prepareDecoder(Sha256(_))
8+
implicit val Sha256Encoder: Encoder[Sha256] = Encoder.encodeString.contramap((s: Sha256) => s.toString)
9+
implicit val MD5Decoder: Decoder[MD5] = prepareDecoder(MD5(_))
10+
implicit val MD5Encoder: Encoder[MD5] = Encoder.encodeString.contramap((h: MD5) => h.toString)
11+
implicit val Sha1Decoder: Decoder[Sha1] = prepareDecoder(Sha1(_))
12+
implicit val Sha1Encoder: Encoder[Sha1] = Encoder.encodeString.contramap((s: Sha1) => s.toString)
13+
14+
private def prepareDecoder[A](parse: String => A): Decoder[A] =
15+
(c: HCursor) => {
16+
c.value.as[String].flatMap { s =>
17+
Either
18+
.catchNonFatal(parse(s))
19+
.leftMap { e =>
20+
DecodingFailure(e.toString, c.history)
21+
}
22+
}
23+
}
24+
}

core/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
archivesBaseName = "scala-hashes_$scalaVersion"

src/main/scala/com/avast/scala/hashes/MD5.scala renamed to core/src/main/scala/com/avast/scala/hashes/MD5.scala

-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.avast.scala.hashes
22

3-
import io.circe.{Decoder, Encoder}
4-
53
import java.util
64

75
case class MD5(bytes: Array[Byte]) {
@@ -21,7 +19,4 @@ case class MD5(bytes: Array[Byte]) {
2119
object MD5 {
2220
private val bytesLength = 16
2321
def apply(hexOrBase64: String): MD5 = MD5(tryHex2bytes(hexOrBase64, bytesLength).getOrElse(base642bytes(hexOrBase64)))
24-
25-
implicit lazy val MD5Decoder: Decoder[MD5] = circe.prepareDecoder(MD5(_))
26-
implicit lazy val MD5Encoder: Encoder[MD5] = Encoder.encodeString.contramap((h: MD5) => h.toString)
2722
}

src/main/scala/com/avast/scala/hashes/Sha1.scala renamed to core/src/main/scala/com/avast/scala/hashes/Sha1.scala

-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.avast.scala.hashes
22

3-
import io.circe.{Decoder, Encoder}
4-
53
import java.util
64

75
case class Sha1(bytes: Array[Byte]) {
@@ -21,7 +19,4 @@ case class Sha1(bytes: Array[Byte]) {
2119
object Sha1 {
2220
private val bytesLength = 20
2321
def apply(hexOrBase64: String): Sha1 = Sha1(tryHex2bytes(hexOrBase64, bytesLength).getOrElse(base642bytes(hexOrBase64)))
24-
25-
implicit lazy val Sha1Decoder: Decoder[Sha1] = circe.prepareDecoder(Sha1(_))
26-
implicit lazy val Sha1Encoder: Encoder[Sha1] = Encoder.encodeString.contramap((s: Sha1) => s.toString)
2722
}

src/main/scala/com/avast/scala/hashes/Sha256.scala renamed to core/src/main/scala/com/avast/scala/hashes/Sha256.scala

-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.avast.scala.hashes
22

3-
import io.circe.{Decoder, Encoder}
4-
53
import java.util
64

75
case class Sha256(bytes: Array[Byte]) {
@@ -21,7 +19,4 @@ case class Sha256(bytes: Array[Byte]) {
2119
object Sha256 {
2220
private val bytesLength = 32
2321
def apply(hexOrBase64: String): Sha256 = Sha256(tryHex2bytes(hexOrBase64, bytesLength).getOrElse(base642bytes(hexOrBase64)))
24-
25-
implicit lazy val Sha256Decoder: Decoder[Sha256] = circe.prepareDecoder(Sha256(_))
26-
implicit lazy val Sha256Encoder: Encoder[Sha256] = Encoder.encodeString.contramap((s: Sha256) => s.toString)
2722
}

settings.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
rootProject.name = 'scala-hashes'
2+
3+
include 'core', 'circe'

src/main/scala/com/avast/scala/hashes/circe/package.scala

-25
This file was deleted.

src/main/scala/com/avast/scala/hashes/json4s/package.scala

-21
This file was deleted.

0 commit comments

Comments
 (0)