aboutsummaryrefslogtreecommitdiffstats
path: root/style-generator/src/Decoder/Legacy.elm
blob: 2c7be93f8fd70639a325fa234984ea94944f8b93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module Decoder.Legacy exposing (filter)

import Decoder.Expression exposing (decodeBool)
import Decoder.Generic as Decode
import Decoder.Helpers exposing (todo)
import Json.Decode as D exposing (Decoder)
import Lib
import MyElm.Syntax exposing (Expression, calln, float, int, list, pair, string)


filter =
    let
        decodeProp =
            D.index 1 D.string
                |> D.map
                    (\prop ->
                        case prop of
                            "$type" ->
                                Lib.eValue "geometryType"

                            "$id" ->
                                Lib.eValue "id"

                            _ ->
                                calln (Lib.eName "getProperty") [ Lib.str (string prop) ]
                    )

        decodeVal =
            D.index 2 <|
                D.oneOf
                    [ D.map (string >> Lib.str) D.string
                    , D.map (float >> Lib.float) D.float
                    , decodeBool
                    ]

        decodeVals =
            D.list <|
                D.oneOf
                    [ D.map (string >> Lib.str) D.string
                    , D.map (float >> Lib.float) D.float
                    , decodeBool
                    ]

        operator name =
            D.map2 (\prop val -> Lib.pipelineCall name [ prop, val ]) decodeProp decodeVal
    in
    D.index 0 D.string
        |> D.andThen
            (\filt ->
                case filt of
                    "all" ->
                        Decode.tail filter |> D.map (\filters -> calln (Lib.eName "all") [ list filters ])

                    "any" ->
                        Decode.tail filter |> D.map (\filters -> calln (Lib.eName "any") [ list filters ])

                    "none" ->
                        Decode.tail filter |> D.map (\filters -> calln (Lib.eName "all") [ list (List.map (\f -> calln (Lib.eName "not") [ f ]) filters) ])

                    "has" ->
                        D.index 1 D.string |> D.map (\prop -> calln (Lib.eName "hasProperty") [ Lib.str (string prop) ])

                    "!has" ->
                        D.index 1 D.string |> D.map (\prop -> calln (Lib.eName "not") [ calln (Lib.eName "hasProperty") [ Lib.str (string prop) ] ])

                    "==" ->
                        operator "isEqual"

                    "!=" ->
                        operator "notEqual"

                    ">" ->
                        operator "greaterThan"

                    ">=" ->
                        operator "greaterThanOrEqual"

                    "<" ->
                        operator "lessThan"

                    "<=" ->
                        operator "lessThanOrEqual"

                    "in" ->
                        D.map2
                            (\prop values ->
                                List.drop 2 values
                                    |> List.map (\v -> Lib.pipelineCall "isEqual" [ prop, v ])
                                    |> list
                                    |> List.singleton
                                    |> calln (Lib.eName "any")
                            )
                            decodeProp
                            decodeVals

                    "!in" ->
                        D.map2
                            (\prop values ->
                                List.drop 2 values
                                    |> List.map (\v -> Lib.pipelineCall "notEqual" [ prop, v ])
                                    |> list
                                    |> List.singleton
                                    |> calln (Lib.eName "all")
                            )
                            decodeProp
                            decodeVals

                    _ ->
                        D.fail "not actually a legacy filter"
            )