Skip to content
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

Fix Json.add and Json.merge operations #129

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ambrusadrianzh
Copy link

Fixes #118

Changes:

  • Json.add should return object with added/updated field
  • Json.merge should work as Map.plus
  • Json.toList should use Pair to Tuple2 function
  • Add test cases for add and merge operations

* Json.add should return object with added/updated field
* Json.merge should work as Map.plus
* Json.toList should use Pair to Tuple2 function
* Add test cases for add and merge operations
Copy link
Contributor

@AdrianRaFo AdrianRaFo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rather think that, after this, both add and merge are the same and we should delete one of them.
Apart from that, I'm my opinion, both methods only works on JsObjects so one of both we should define the methods on each Json type or move them to JsObject. This second thing could be discussed on a different issue and done on a different PR if you like

helios-core/src/main/kotlin/helios/core/helios.kt Outdated Show resolved Hide resolved
@ambrusadrianzh
Copy link
Author

Agreed, add and merge should only work on JsObjects.

However, I'm not sure if add and merge serve the same purpose. With add you can add JsString, JsNumber etc. to a JsObject with a certain key.

The merge function should merge two JsObjects together. This way we can avoid the following confusing situtations:

val a = JsObject("weight" to JsFloat(61.4f))
val b = JsString("abc")

val c = a.merge(b) // c == b for no reason

If we constrain the merge function to JsObjects only, these situations will not able to happen. I'm happy to make these changes, if we can agree on these.

@AdrianRaFo
Copy link
Contributor

As it is on this PR, merge serves the same purpose than add because if, continuing with your example, a.merge(b) doesn't do anything because b has to be a JsObject.
You can restrict that making merge to receive a JsObject directly and it would be the same as add with the insignificant difference that add just allow you to add new values one by one and merge would allow you to add them all at the same call.

@AdrianRaFo
Copy link
Contributor

AdrianRaFo commented Oct 23, 2019

I agree to move them to JsObject but I would like the opinion of @nomisRev here too

@ambrusadrianzh
Copy link
Author

@AdrianRaFo @nomisRev How should we proceed? 🤔

Copy link
Contributor

@nomisRev nomisRev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very excited to see contributions to Helios! 👏👏👏

helios-core/src/main/kotlin/helios/core/helios.kt Outdated Show resolved Hide resolved
helios-core/src/main/kotlin/helios/core/helios.kt Outdated Show resolved Hide resolved
* move add operation from Json to JsObject
* add Monoid to Json.merge operation
@ambrusadrianzh
Copy link
Author

Hey @nomisRev,

Sorry for the delay. Made the changes today, caught up with upstream and integrated the testing changes from arrow test. Please take a look at it, as I'm not sure I got it right.

helios-core/build.gradle Show resolved Hide resolved

fun JsObject.Companion.semigroup(): JsObjectSemigroup = object : JsObjectSemigroup { }

fun JsObject.Companion.monoid(): JsObjectMonoid = object : JsObjectMonoid { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm missing JsNull, JsDecimal, JsNumber, and JsBoolean, also maybe we can add a Monoid of Json matching over all these monoids

JsString.eq()
)
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tests for each monoid?

)

testLaws(
MonoidLaws.laws(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess Monoid laws also includes Semigroup laws

override fun Option<Json>.combine(b: Option<Json>) = flatMap { a -> b.map { a.merge(it) } }
override fun empty(): Option<Json> = None
}
))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we deleting the Lens law test?

}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be updated, not deleted

@nomisRev
Copy link
Contributor

nomisRev commented Dec 3, 2019

@ambrusadrianzh no worries! Thank you for the contributions! I saw @AdrianRaFo already checked out your PR, I can give it a look next week. This week is a little busy with travelling and KotlinConf ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weird 'add' function
4 participants