-
Notifications
You must be signed in to change notification settings - Fork 22
Fix Json.add and Json.merge operations #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package helios.instances | ||
|
|
||
| import arrow.extension | ||
| import arrow.typeclasses.Monoid | ||
| import helios.core.JsArray | ||
| import helios.core.JsDouble | ||
| import helios.core.JsFloat | ||
| import helios.core.JsInt | ||
| import helios.core.JsLong | ||
| import helios.core.JsObject | ||
| import helios.core.JsString | ||
|
|
||
| @extension | ||
| interface JsIntMonoid : Monoid<JsInt>, JsIntSemigroup { | ||
| override fun empty(): JsInt = JsInt(0) | ||
| } | ||
|
|
||
| @extension | ||
| interface JsLongMonoid : Monoid<JsLong>, JsLongSemigroup { | ||
| override fun empty(): JsLong = JsLong(0L) | ||
| } | ||
|
|
||
| @extension | ||
| interface JsFloatMonoid : Monoid<JsFloat>, JsFloatSemigroup { | ||
| override fun empty(): JsFloat = JsFloat(0f) | ||
| } | ||
|
|
||
| @extension | ||
| interface JsDoubleMonoid : Monoid<JsDouble>, JsDoubleSemigroup { | ||
| override fun empty(): JsDouble = JsDouble(0.0) | ||
| } | ||
|
|
||
| @extension | ||
| interface JsStringMonoid : Monoid<JsString>, JsStringSemigroup { | ||
| override fun empty(): JsString = JsString("") | ||
| } | ||
|
|
||
| @extension | ||
| interface JsArrayMonoid : Monoid<JsArray>, JsArraySemigroup { | ||
| override fun empty(): JsArray = JsArray(emptyList()) | ||
| } | ||
|
|
||
| @extension | ||
| interface JsObjectMonoid : Monoid<JsObject>, JsObjectSemigroup { | ||
| override fun empty(): JsObject = JsObject(emptyMap()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package helios.instances | ||
|
|
||
| import arrow.core.ListK | ||
| import arrow.core.extensions.listk.semigroup.semigroup | ||
| import arrow.core.extensions.semigroup | ||
| import arrow.core.k | ||
| import arrow.extension | ||
| import arrow.typeclasses.Semigroup | ||
| import helios.core.JsArray | ||
| import helios.core.JsDouble | ||
| import helios.core.JsFloat | ||
| import helios.core.JsInt | ||
| import helios.core.JsLong | ||
| import helios.core.JsObject | ||
| import helios.core.JsString | ||
| import helios.core.Json | ||
|
|
||
| @extension | ||
| interface JsIntSemigroup : Semigroup<JsInt> { | ||
| override fun JsInt.combine(b: JsInt): JsInt = JsInt(Int.semigroup().run { | ||
| value.combine(b.value) | ||
| }) | ||
| } | ||
|
|
||
| @extension | ||
| interface JsLongSemigroup : Semigroup<JsLong> { | ||
| override fun JsLong.combine(b: JsLong): JsLong = JsLong(Long.semigroup().run { | ||
| value.combine(b.value) | ||
| }) | ||
| } | ||
|
|
||
| @extension | ||
| interface JsFloatSemigroup : Semigroup<JsFloat> { | ||
| override fun JsFloat.combine(b: JsFloat): JsFloat = JsFloat(Float.semigroup().run { | ||
| value.combine(b.value) | ||
| }) | ||
| } | ||
|
|
||
| @extension | ||
| interface JsDoubleSemigroup : Semigroup<JsDouble> { | ||
| override fun JsDouble.combine(b: JsDouble): JsDouble = JsDouble(Double.semigroup().run { | ||
| value.combine(b.value) | ||
| }) | ||
| } | ||
|
|
||
| @extension | ||
| interface JsStringSemigroup : Semigroup<JsString> { | ||
| override fun JsString.combine(b: JsString): JsString = JsString(String.semigroup().run { | ||
| value.toString().combine(b.value.toString()) | ||
| }) | ||
| } | ||
|
|
||
| @extension | ||
| interface JsArraySemigroup : Semigroup<JsArray> { | ||
| override fun JsArray.combine(b: JsArray): JsArray = JsArray(ListK.semigroup<Json>().run { | ||
| value.k().combine(b.value.k()) | ||
| }) | ||
| } | ||
|
|
||
| @extension | ||
| interface JsObjectSemigroup : Semigroup<JsObject> { | ||
| override fun JsObject.combine(b: JsObject): JsObject = JsObject(value + b.value) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package helios.core | ||
|
|
||
| import arrow.test.UnitSpec | ||
| import arrow.test.laws.MonoidLaws | ||
| import arrow.test.laws.SemigroupLaws | ||
| import helios.instances.jsstring.eq.eq | ||
| import helios.instances.jsstring.monoid.monoid | ||
| import helios.instances.jsstring.semigroup.semigroup | ||
| import io.kotlintest.properties.Gen | ||
|
|
||
| class MonoidTest : UnitSpec() { | ||
|
|
||
| init { | ||
| testLaws( | ||
| SemigroupLaws.laws( | ||
| JsString.semigroup(), | ||
| JsString("a"), | ||
| JsString("b"), | ||
| JsString("c"), | ||
| JsString.eq() | ||
| ) | ||
| ) | ||
|
|
||
| testLaws( | ||
| MonoidLaws.laws( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess Monoid laws also includes Semigroup laws |
||
| JsString.monoid(), | ||
| Gen.string().map { JsString(it) }, | ||
| JsString.eq() | ||
| ) | ||
| ) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A tests for each monoid? |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,14 @@ | ||
| package helios.optics | ||
|
|
||
| import arrow.core.None | ||
| import arrow.core.Option | ||
| import arrow.core.extensions.option.eq.eq | ||
| import arrow.typeclasses.Eq | ||
| import helios.arrow.UnitSpec | ||
| import helios.arrow.generators.functionAToB | ||
| import helios.arrow.generators.option | ||
| import helios.arrow.laws.LensLaws | ||
| import helios.arrow.laws.OptionalLaws | ||
| import helios.arrow.laws.TraversalLaws | ||
| import arrow.typeclasses.Eq | ||
| import arrow.typeclasses.Monoid | ||
| import helios.core.JsArray | ||
| import helios.core.JsObject | ||
| import helios.core.Json | ||
| import helios.instances.jsobject.eq.eq | ||
| import helios.instances.json.eq.eq | ||
| import helios.optics.jsarray.each.each | ||
| import helios.optics.jsarray.index.index | ||
| import helios.optics.jsobject.at.at | ||
| import helios.optics.jsobject.each.each | ||
| import helios.optics.jsobject.index.index | ||
| import helios.test.generators.alphaStr | ||
|
|
@@ -29,66 +19,52 @@ import io.kotest.properties.Gen | |
|
|
||
| class InstancesTest : UnitSpec() { | ||
|
|
||
| init { | ||
|
|
||
| testLaws( | ||
| OptionalLaws.laws( | ||
| optionalGen = Gen.alphaStr().map { JsObject.index().index(it) }, | ||
| aGen = Gen.jsObject(), | ||
| bGen = Gen.json(), | ||
| funcGen = Gen.functionAToB(Gen.json()), | ||
| EQA = Eq.any(), | ||
| EQOptionB = Eq.any() | ||
| ) | ||
| ) | ||
|
|
||
| testLaws( | ||
| OptionalLaws.laws( | ||
| optional = JsArray.index().index(1), | ||
| aGen = Gen.jsArray(), | ||
| bGen = Gen.json(), | ||
| funcGen = Gen.functionAToB(Gen.json()), | ||
| EQA = Eq.any(), | ||
| EQOptionB = Eq.any() | ||
| ) | ||
| ) | ||
| init { | ||
|
|
||
| testLaws( | ||
| TraversalLaws.laws( | ||
| traversal = JsObject.each().each(), | ||
| aGen = Gen.jsObject(), | ||
| bGen = Gen.json(), | ||
| funcGen = Gen.functionAToB(Gen.json()), | ||
| EQA = Eq.any(), | ||
| EQOptionB = Eq.any(), | ||
| EQListB = Eq.any() | ||
| ) | ||
| ) | ||
| testLaws( | ||
| OptionalLaws.laws( | ||
| optionalGen = Gen.alphaStr().map { JsObject.index().index(it) }, | ||
| aGen = Gen.jsObject(), | ||
| bGen = Gen.json(), | ||
| funcGen = Gen.functionAToB(Gen.json()), | ||
| EQA = Eq.any(), | ||
| EQOptionB = Eq.any() | ||
| ) | ||
| ) | ||
|
|
||
| testLaws( | ||
| TraversalLaws.laws( | ||
| traversal = JsArray.each().each(), | ||
| aGen = Gen.jsArray(), | ||
| bGen = Gen.json(), | ||
| funcGen = Gen.functionAToB(Gen.json()), | ||
| EQA = Eq.any(), | ||
| EQOptionB = Eq.any(), | ||
| EQListB = Eq.any() | ||
| ) | ||
| ) | ||
| testLaws( | ||
| OptionalLaws.laws( | ||
| optional = JsArray.index().index(1), | ||
| aGen = Gen.jsArray(), | ||
| bGen = Gen.json(), | ||
| funcGen = Gen.functionAToB(Gen.json()), | ||
| EQA = Eq.any(), | ||
| EQOptionB = Eq.any() | ||
| ) | ||
| ) | ||
|
|
||
| testLaws(LensLaws.laws( | ||
| lensGen = Gen.alphaStr().map { JsObject.at().at(it) }, | ||
| aGen = Gen.jsObject(), | ||
| bGen = Gen.option(Gen.json()), | ||
| funcGen = Gen.functionAToB(Gen.option(Gen.json())), | ||
| EQA = JsObject.eq(), | ||
| EQB = Option.eq(Json.eq()), | ||
| MB = object : Monoid<Option<Json>> { | ||
| override fun Option<Json>.combine(b: Option<Json>) = flatMap { a -> b.map { a.merge(it) } } | ||
| override fun empty(): Option<Json> = None | ||
| } | ||
| )) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we deleting the Lens law test? |
||
| testLaws( | ||
| TraversalLaws.laws( | ||
| traversal = JsObject.each().each(), | ||
| aGen = Gen.jsObject(), | ||
| bGen = Gen.json(), | ||
| funcGen = Gen.functionAToB(Gen.json()), | ||
| EQA = Eq.any(), | ||
| EQOptionB = Eq.any(), | ||
| EQListB = Eq.any() | ||
| ) | ||
| ) | ||
|
|
||
| } | ||
| testLaws( | ||
| TraversalLaws.laws( | ||
| traversal = JsArray.each().each(), | ||
| aGen = Gen.jsArray(), | ||
| bGen = Gen.json(), | ||
| funcGen = Gen.functionAToB(Gen.json()), | ||
| EQA = Eq.any(), | ||
| EQOptionB = Eq.any(), | ||
| EQListB = Eq.any() | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.