Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds marshal and unmarshal for constraints (see #130). I tried to built it similar to the already existing implementations for the version type, but there were some challenges
Challenges
json.Marshal escapes certain characters
Unlike version, constraints can also contain '<' and '>' characters. Unfortunately these get escaped when using
json.Marshal
directly (note theescapeHTML
option in source). As a result we cannot simply calljson.Marshal
inside our custom MarshalJSON. Therefore I have decided to use a custom encoder that disables HTML escaping.So far so good. Unfortunately our troubles do not end there. As a matter of fact
encoding.json
calls additional logic after it has called our custom Marshaler. In our case, the troublesome part is thecompact
func. The main issue here is that it runs its own escaper, which we cannot overwrite. As a result if you are marshalling usingjson.Marshal
, it will always contain the unicode runes for them (e.g. "\u003c" for "<").Implications for end-users
I think for most users this should be fine, as you can use the escaped characters directly within semVer. For example you can use them in the
NewConstraint
func without any issues:And if that is not enough, users can always use a custom Encoder with
SetEscapeHTML
disabled instead ofjson.Marshal
. This is similar to how we do it in the unit test:Open Questions
TestNewConstraint
which contain escaped characters?