Skip to content

Commit

Permalink
V8.0.2 (#135)
Browse files Browse the repository at this point in the history
* test: πŸ’ test compose optional lenses

Closes: close #134

* docs: ✏️ changelog

* docs: ✏️ javadoc

* ci: 🎑 version release
  • Loading branch information
imrafaelmerino committed Jun 25, 2020
1 parent dee5c89 commit d5ccbef
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 48 deletions.
7 changes: 3 additions & 4 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# JSON-VALUES
## v8.0.1 ( Wed Jun 17 2020 23:12:43 GMT+0200 (Central European Summer Time) )
## v8.0.2 ( Thu Jun 25 2020 22:54:24 GMT+0200 (Central European Summer Time) )


## Features
- 🎸 set with padding
([713823ad](https://github.com/imrafaelmerino/json-values/commit/713823adab0f3a769748b122a514048de64ae61a))
## Feature
- Optionals and Lenses can be composed



Expand Down
49 changes: 6 additions & 43 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

[![Javadocs](https://www.javadoc.io/badge/com.github.imrafaelmerino/json-values.svg)](https://www.javadoc.io/doc/com.github.imrafaelmerino/json-values)
[![Maven](https://img.shields.io/maven-central/v/com.github.imrafaelmerino/json-values/8.0.1)](https://search.maven.org/artifact/com.github.imrafaelmerino/json-values/8.0.1/jar)
[![Maven](https://img.shields.io/maven-central/v/com.github.imrafaelmerino/json-values/8.0.2)](https://search.maven.org/artifact/com.github.imrafaelmerino/json-values/8.0.2/jar)
[![](https://jitpack.io/v/imrafaelmerino/json-values.svg)](https://jitpack.io/#imrafaelmerino/json-values)

[![Gitter](https://badges.gitter.im/json-values/community.svg)](https://gitter.im/json-values/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
Expand All @@ -21,7 +21,6 @@
- [Develop](#develop)
- [Related projects](#rp)


## <a name="introduction"><a/> Introduction
Welcome to **json-values**, the first-ever Json library in _Java_ implemented with persistent data structures.

Expand Down Expand Up @@ -180,41 +179,11 @@ JsObj obj = supplier.get();
```

I've written about json-values on my [blog](http://blog.imrafaelmerino.dev):
* [The value of json values - Recursive data structures](http://blog.imrafaelmerino.dev/2020/06/the-value-of-json-values-recursive-data.html)
* [The value of json values - Optics](https://blog.imrafaelmerino.dev/2020/06/the-value-of-json-values-optics.html)

We can use optics to put data in and get data out in a composable and concise way.Lenses, Optionals, and Prism have been
defined for every json type.

```
Lens<JsObj,String> nameLens = JsObj.lens.str("name");
Lens<JsObj,Integer> ageLens = JsObj.lens.intNum("age");
Lens<JsObj,JsArray> languagesLens = JsObj.lens.array("languages");
Option<JsObj, String> githubOpt = JsObj.optional.str("name");
Lens<JsObj,String> cityLens = JsObj.lens.str(path("/address/city"));
Lens<JsObj,Double> latitudeLens = JsObj.lens.doubleNum(path("/address/location/0"));
Lens<JsObj,Double> longitudeLens = JsObj.lens.doubleNum(path("/address/location/1"));
Lens<JsObj,JsValue> countryLens = JsObj.lens.value(path("/address/country"));
Function<Integer,Function<JsObj,JsObj>> incAge =
i -> ageLens.modify.apply(n -> n+i);
Function<String,Function<JsObj,JsObj>> addLanguage =
lan -> languagesLens.modify.apply(a -> a.append(JsStr.of(lan)));
Function<Function<Double,Double>,Function<JsObj,JsObj>> modifyLatitude = latitudeLens.modify::apply;
Function<Function<Double,Double>,Function<JsObj,JsObj>> modifyLongitude = longitudeLens.modify::apply;
Function<String,Function<JsObj,JsObj>> setCountry = c -> countryLens.set.apply(JsStr.of(c));
Function<String,Function<JsObj,JsObj>> setName = nameLens.set::apply;
Function<JsObj,JsObj> fn = setName.apply("Philip").andThen(incAge.apply(1))
.andThen(addLanguage.apply("Lisp"))
.andThen(setCountry.apply("ES"))
.andThen(modifyLatitude.apply(l -> l + 0.5))
.andThen(modifyLongitude.apply(l -> l + 0.8));
var newPerson = fn.apply(person);
```

This is just a quick intro, but I'd like to highlight that generators, specs and optics are really powerful and composable.
Furthermore, any spec can be defined in terms of predicates, which allows you to define any imaginable validation.

## <a name="notwhatfor"><a/> When not to use it
**json-values** fits well in _pure_ OOP and incredibly well in FP, but NOT in _EOOP_, which stands for
Expand All @@ -229,16 +198,13 @@ Add the following dependency to your building tool:
<dependency>
<groupId>com.github.imrafaelmerino</groupId>
<artifactId>json-values</artifactId>
<version>8.0.1</version>
<version>8.0.2</version>
</dependency>
```

## <a name="wth"><a/> Want to help
I've set up a separate document for [contributors](./CONTRIBUTING.md).
## <a name="develop"><a/> Develop
I've set up a separate document for [developers](./developers.md). Things like why json-values is a one-package library, if it was developed using TDD or anything related to the
development of the library can be found there. I'll be adding little by little more and more
information.

## <a name="rp"><a/> Related projects
β€œIdeas are like rabbits. You get a couple and learn how to handle them, and pretty soon you have a dozen.” – John Steinbeck

Expand All @@ -251,6 +217,3 @@ whole wide world! If I'm wrong, please let me know!
json-values uses the persistent data structures from [vavr](https://www.vavr.io/), [Jackson](https://github.com/FasterXML/jackson) to parse a string/bytes into
a stream of tokens and [dsl-sjon](https://github.com/ngs-doo/dsl-json) to parse a string/bytes given a spec.

I've written about json-values on my blog:
* [The value of json values - Recursive data structures](http://blog.imrafaelmerino.dev/2020/06/the-value-of-json-values-recursive-data.html)

3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.github.imrafaelmerino</groupId>
<artifactId>json-values</artifactId>
<packaging>jar</packaging>
<version>8.0.1</version>
<version>8.0.2</version>

<name>json-values</name>
<description>json-values is a functional Java library to work with immutable jsons using persistent data structures.</description>
Expand Down Expand Up @@ -67,6 +67,7 @@
<artifactId>dsl-json</artifactId>
<version>${dsl-json.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.jsoniter/jsoniter -->

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/jsonvalues/Lens.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public class Lens<S, O> {
}


/**
Composing a Lens and a Prism returns and Optional
@param prism A Prism from the focus of the lens to the new focus of the Optional
@param <T> the type of the new focus of the Optional
@return an Optional
*/
public <T> Option<S, T> compose(final Prism<O, T> prism) {
return new Option<>(json -> requireNonNull(prism).getOptional.apply(get.apply(json)),
value -> json -> set.apply(prism.reverseGet.apply(value))
Expand All @@ -69,4 +75,20 @@ public <T> Option<S, T> compose(final Prism<O, T> prism) {

}

/**
Compose this lens with another one
@param other the other lens
@param <B> the type of the focus on the new lens
@return a new Lens
*/
public <B> Lens<S,B> compose(final Lens<O,B> other){

return new Lens<>(this.get.andThen(other.get),
b-> s -> {
O o = other.set.apply(b).apply(this.get.apply(s));
return this.set.apply(o).apply(s);
}
);
}

}
19 changes: 19 additions & 0 deletions src/main/java/jsonvalues/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ public class Option<S, T> {
};
}


/**
compose this optional with the given as parameter
@param other the other optional
@param <F> the type of the focus
@return a new optional
*/
public <F> Option<S,F> compose(final Option<T,F> other){
return new Option<>(s -> {
Optional<T> t = this.get.apply(s);
if(t.isPresent()) return other.get.apply(t.get());
else return Optional.empty();
}, f -> s -> {
Optional<T> t = this.get.apply(s);
if(t.isPresent()) return this.set.apply(other.set.apply(f).apply(t.get())).apply(s);
else return s;
});
}

}


Expand Down
26 changes: 26 additions & 0 deletions src/test/java/jsonvalues/TestLenses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package jsonvalues;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TestLenses {


@Test
public void testCompose(){

Lens<JsObj,JsObj> address = JsObj.lens.obj("address");

Lens<JsObj,String> street = JsObj.lens.str("street");

Lens<JsObj, String> compose = address.compose(street);

JsObj obj = JsObj.of("address",
JsObj.of("street",
JsStr.of("a")));

Assertions.assertEquals("a",compose.get.apply(obj));

Assertions.assertEquals("b",compose.get.apply(compose.set.apply("b").apply(obj)));
}
}
57 changes: 57 additions & 0 deletions src/test/java/jsonvalues/TestOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package jsonvalues;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Optional;

public class TestOption {


@Test
public void testCompose(){

Option<JsObj, JsObj> address = JsObj.optional.obj("address");

Option<JsObj, String> street = JsObj.optional.str("street");

Option<JsObj, String> compose = address.compose(street);

JsObj obj = JsObj.of("address",
JsObj.of("street",
JsStr.of("a")));

Assertions.assertEquals(Optional.of("a"), compose.get.apply(obj));

Assertions.assertEquals(Optional.of("b"),compose.get.apply(compose.set.apply("b").apply(obj)));
}

@Test
public void testComposeEmpty(){

Option<JsObj, JsObj> address = JsObj.optional.obj("address");

Option<JsObj, String> street = JsObj.optional.str("street");

Option<JsObj, String> compose = address.compose(street);

JsObj obj = JsObj.empty();

Assertions.assertEquals(Optional.empty(), compose.get.apply(obj));

Assertions.assertEquals(obj,compose.set.apply("b").apply(obj));

}




Option<JsObj,JsObj> address = JsObj.optional.obj("address");;

Option<JsObj,JsArray> coordinates = JsObj.optional.array("coordinates");

Option<JsArray,Double> latitude = JsArray.optional.doubleNum(0);

Option<JsObj, Double> personLatitude = address.compose(coordinates)
.compose(latitude);
}

0 comments on commit d5ccbef

Please sign in to comment.