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

WIP: Tryin' to work out elm-json-accessors 😬 #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"andre-dietrich/elm-generic": "2.0.0 <= v < 3.0.0",
"elm/core": "1.0.0 <= v < 2.0.0",
"elm/json": "1.0.0 <= v < 2.0.0",
"lue-bird/elm-rosetree-path": "1.1.0 <= v < 2.0.0",
"miyamoen/select-list": "4.1.0 <= v < 5.0.0",
"zwilias/elm-rosetree": "1.5.0 <= v < 2.0.0"
Expand Down
2 changes: 1 addition & 1 deletion src/Base.elm
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ prism n bt sta sub =
{ view = void "Can't call `view` with a Prism"
, list =
sta
>> Result.map (\a -> [ a ])
>> Result.map List.singleton
>> Result.withDefault []
, make = bt
, over = over_
Expand Down
135 changes: 132 additions & 3 deletions src/Json/Accessors.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,134 @@
module Json.Accessors exposing (..)
module Json.Accessors exposing
( at
, bool_
, float_
, int_
, key
, list_
, null_
, object_
, string_
, value_
)

import Base exposing (..)
import Dict exposing (Dict)
import Dict.Accessors as Dict
import Generic
import Generic.Json as Json
import Json.Encode as Encode
import List.Accessors as List

stubb =
Debug.todo "make jq expressions from Lenses?"

value_ : Optic pr ls Generic.Value Generic.Value x y -> Prism pr String String x y
value_ =
prism "JSON"
(Json.encode >> Encode.encode 0)
(\v -> Json.decode v |> Result.mapError (\_ -> v))


int_ : Optic pr ls Int Int x y -> Prism pr String String x y
int_ =
value_
<< prism "int"
Generic.Int
(Generic.toInt
>> Maybe.map Ok
>> Maybe.withDefault (Err Generic.Null)
)


float_ : Optic pr ls Float Float x y -> Prism pr String String x y
float_ =
value_
<< prism "float"
Generic.Float
(Generic.toFloat
>> Maybe.map Ok
>> Maybe.withDefault (Err Generic.Null)
)


string_ : Optic pr ls String String x y -> Prism pr String String x y
string_ =
value_
<< prism "string"
Generic.String
(\v ->
case v of
Generic.String s ->
Ok s

_ ->
Err v
)


bool_ : Optic pr ls Bool Bool x y -> Prism pr String String x y
bool_ =
value_
<< prism "bool"
Generic.Bool
(\v ->
case v of
Generic.Bool b ->
Ok b

_ ->
Err v
)


null_ : Optic pr ls () b x y -> Prism pr String String x y
null_ =
value_
<< prism "null"
(always Generic.Null)
(\v ->
case v of
Generic.Null ->
Ok ()

_ ->
Err v
)


list_ : Optic pr ls (List Generic.Value) (List Generic.Value) x y -> Prism pr String String x y
list_ =
value_
<< prism "List"
Generic.List
(\v ->
case v of
Generic.List a ->
Ok a

_ ->
Err v
)


object_ : Optic pr ls (Dict String Generic.Value) (Dict String Generic.Value) x y -> Prism pr String String x y
object_ =
value_
<< prism "Dict"
(Dict.toList >> (List.map << Tuple.mapFirst) Generic.String >> Generic.dictFromList)
(\v ->
case Generic.toDict v of
Just o ->
Ok o

Nothing ->
Err v
)


key : String -> Optic pr ls (Maybe Generic.Value) (Maybe Generic.Value) x y -> Traversal String String x y
key s =
object_ << Dict.at s


at : Int -> Optic pr ls Generic.Value Generic.Value x y -> Traversal String String x y
at i =
list_ << List.at i
4 changes: 2 additions & 2 deletions tests/Laws.elm
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ iso_yon l a =



-- traverse_pure : LensLike' f s a -> s -> Bool
-- traverse_pure l s = l pure s == (pure s : f s)
-- traverse_pure : Applicative f => LensLike' f s a -> s -> Bool
-- traverse_pure l s = l pure s == (pure s :: f s) -- :: is an inline type annotation
-- traverse_pureMaybe : Eq s => LensLike' Maybe s a -> s -> Bool
-- traverse_pureMaybe = traverse_pure
-- traverse_pureList : Eq s => LensLike' [] s a -> s -> Bool
Expand Down