File tree Expand file tree Collapse file tree 8 files changed +76
-9
lines changed Expand file tree Collapse file tree 8 files changed +76
-9
lines changed Original file line number Diff line number Diff line change @@ -18,10 +18,12 @@ package dev.hnaderi.k8s
18
18
package circe
19
19
20
20
import dev .hnaderi .k8s .utils .Builder
21
- import io .circe ._
21
+ import io .circe .Json
22
22
import io .circe .syntax ._
23
23
24
24
private [circe] object CirceBuilder extends Builder [Json ] {
25
+ /* NOTE that this import is required to use correct implicit instances */
26
+ import io .circe .Encoder ._
25
27
26
28
override def of (str : String ): Json = str.asJson
27
29
Original file line number Diff line number Diff line change 17
17
package dev .hnaderi .k8s .circe
18
18
19
19
import dev .hnaderi .k8s .utils .Reader
20
- import io .circe ._
20
+ import io .circe .Decoder
21
+ import io .circe .Json
22
+ import io .circe .JsonObject
21
23
22
24
private [circe] object CirceReader extends Reader [Json ] {
23
25
/* NOTE that this import is required to use correct implicit instances */
Original file line number Diff line number Diff line change 16
16
17
17
package dev .hnaderi .k8s .circe
18
18
19
+ import dev .hnaderi .k8s .KObject
20
+ import dev .hnaderi .k8s .scalacheck .Generators .arbitraryKObjects
19
21
import dev .hnaderi .k8s .test .CodecSuite
20
22
import io .circe .Json
23
+ import io .circe .syntax ._
24
+ import org .scalacheck .Prop .forAll
21
25
22
- class CirceSuite extends CodecSuite [Json ]
26
+ class CirceSuite extends CodecSuite [Json ] {
27
+ property(" Circe json must be reversible" ) {
28
+ forAll { (obj : KObject ) =>
29
+ val json = obj.asJson
30
+ val dec = json.as[KObject ]
31
+ assertEquals(dec, Right (obj))
32
+ }
33
+ }
34
+ }
Original file line number Diff line number Diff line change 16
16
17
17
package dev .hnaderi .k8s
18
18
19
- import dev .hnaderi .k8s .utils .Reader
19
+ import dev .hnaderi .k8s .utils .Builder
20
20
import dev .hnaderi .k8s .utils .Decoder
21
+ import dev .hnaderi .k8s .utils .Encoder
22
+ import dev .hnaderi .k8s .utils .Reader
21
23
22
24
final case class ResourceKind (
23
25
group : String ,
@@ -39,4 +41,8 @@ trait KObject extends Serializable with Product {
39
41
object KObject {
40
42
implicit def decoder [T : Reader ]: Decoder [T , KObject ] =
41
43
ResourceCodecs .resourceDecoder
44
+ implicit def encoder [T : Builder ]: Encoder [KObject , T ] =
45
+ new Encoder [KObject , T ] {
46
+ def apply (r : KObject ): T = r.foldTo[T ]
47
+ }
42
48
}
Original file line number Diff line number Diff line change 16
16
17
17
package dev .hnaderi .k8s .playJson
18
18
19
- import play .api .libs .json .JsValue
19
+ import dev .hnaderi .k8s .KObject
20
+ import dev .hnaderi .k8s .scalacheck .Generators .arbitraryKObjects
20
21
import dev .hnaderi .k8s .test .CodecSuite
22
+ import org .scalacheck .Prop .forAll
23
+ import play .api .libs .json ._
21
24
22
- class PlayJsonSuite extends CodecSuite [JsValue ]
25
+ class PlayJsonSuite extends CodecSuite [JsValue ] {
26
+ property(" Play json must be reversible" ) {
27
+ forAll { (obj : KObject ) =>
28
+ val json = Json .toJson(obj)
29
+ val dec = json.validate[KObject ]
30
+ assertEquals(dec, JsSuccess (obj))
31
+ }
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2022 Hossein Naderi
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package dev .hnaderi .k8s .sprayJson
18
+
19
+ final case class DecodeError (msg : String ) extends Exception (msg)
Original file line number Diff line number Diff line change @@ -22,7 +22,11 @@ import spray.json._
22
22
package object sprayJson {
23
23
implicit val sprayJsonBuilder : Builder [JsValue ] = SprayBuilder
24
24
implicit val sprayJsonReader : Reader [JsValue ] = SprayReader
25
- implicit def k8sJsonFormat [T ](implicit
25
+ implicit def k8sJsonWriter [T ](implicit
26
26
enc : Encoder [T , JsValue ]
27
27
): JsonWriter [T ] = JsonWriter .func2Writer(enc(_))
28
+ implicit def k8sJsonReader [T ](implicit
29
+ dec : Decoder [JsValue , T ]
30
+ ): JsonReader [T ] =
31
+ JsonReader .func2Reader(dec(_).fold(err => throw DecodeError (err), identity))
28
32
}
Original file line number Diff line number Diff line change 16
16
17
17
package dev .hnaderi .k8s .sprayJson
18
18
19
- import spray .json ._
19
+ import dev .hnaderi .k8s .KObject
20
+ import dev .hnaderi .k8s .scalacheck .Generators .arbitraryKObjects
20
21
import dev .hnaderi .k8s .test .CodecSuite
22
+ import org .scalacheck .Prop .forAll
23
+ import spray .json ._
21
24
22
- class SprayJsonSuite extends CodecSuite [JsValue ]
25
+ class SprayJsonSuite extends CodecSuite [JsValue ] {
26
+ property(" Spray json must be reversible" ) {
27
+ forAll { (obj : KObject ) =>
28
+ val json = obj.toJson
29
+ val dec = json.convertTo[KObject ]
30
+ assertEquals(dec, obj)
31
+ }
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments