Created
December 2, 2018 14:45
-
-
Save ababup1192/555c9acb5cba86da81a62098d21a32f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Main exposing (OriginalFoo, json2OriginalFoo) | |
| import Browser | |
| import Html exposing (Html, div, h1, img, text) | |
| import Html.Attributes exposing (src) | |
| import Json.Decode as D exposing (Decoder) | |
| import Task as Task exposing (Task) | |
| type alias OriginalFoo = | |
| { barTextMaybe : Maybe String | |
| , mustNum : Int | |
| } | |
| type alias Foo = | |
| { barText : String | |
| , mustNum : Int | |
| } | |
| type alias Xxx = | |
| { barText : String | |
| } | |
| json2OriginalFoo : String -> Result D.Error OriginalFoo | |
| json2OriginalFoo json = | |
| D.decodeString originalFoo json | |
| getFoo : Task D.Error String -> Task D.Error String -> Task D.Error Foo | |
| getFoo api1 api2 = | |
| Task.map (D.decodeString originalFoo) api1 | |
| |> Task.andThen | |
| (\orgFooRes -> | |
| case orgFooRes of | |
| Ok { barTextMaybe, mustNum } -> | |
| case barTextMaybe of | |
| Just barText -> | |
| Task.succeed (Foo barText mustNum) | |
| Nothing -> | |
| Task.map (D.decodeString xxx) api2 | |
| |> Task.andThen | |
| (\xxxRes -> | |
| case xxxRes of | |
| Ok { barText } -> | |
| Task.succeed (Foo barText mustNum) | |
| Err err -> | |
| Task.fail err | |
| ) | |
| Err err -> | |
| Task.fail err | |
| ) | |
| originalFoo : Decoder OriginalFoo | |
| originalFoo = | |
| D.map2 OriginalFoo | |
| (D.maybe (D.at [ "foo", "bar" ] D.string)) | |
| (D.field "must" D.int) | |
| xxx : Decoder Xxx | |
| xxx = | |
| D.map Xxx | |
| (D.at [ "xxx", "bar" ] D.string) | |
| fooMustApi : Task x String | |
| fooMustApi = | |
| Task.succeed | |
| """ | |
| { "foo": { "bar": "firstText"}, "must": 1 } | |
| """ | |
| keyMustApi : Task x String | |
| keyMustApi = | |
| Task.succeed | |
| """ | |
| { "key": "key1", "must": 2 } | |
| """ | |
| xxxApi : Task x String | |
| xxxApi = | |
| Task.succeed | |
| """ | |
| { "xxx": { "bar": "secondText" } } | |
| """ | |
| ---- MODEL ---- | |
| type alias Model = | |
| { foo : Foo } | |
| init : () -> ( Model, Cmd Msg ) | |
| init _ = | |
| let | |
| task = | |
| getFoo keyMustApi xxxApi | |
| in | |
| ( { foo = Foo "no" -1 }, Task.attempt GotFoo task ) | |
| ---- UPDATE ---- | |
| type Msg | |
| = NoOp | |
| | GotFoo (Result D.Error Foo) | |
| update : Msg -> Model -> ( Model, Cmd Msg ) | |
| update msg model = | |
| case msg of | |
| NoOp -> | |
| ( model, Cmd.none ) | |
| GotFoo fooRes -> | |
| case fooRes of | |
| Ok foo -> | |
| ( { model | foo = foo }, Cmd.none ) | |
| Err _ -> | |
| ( model, Cmd.none ) | |
| ---- VIEW ---- | |
| view : Model -> Html Msg | |
| view { foo } = | |
| let | |
| { barText, mustNum } = | |
| foo | |
| in | |
| h1 [] [ text <| String.repeat mustNum barText ] | |
| subscriptions : Model -> Sub Msg | |
| subscriptions model = | |
| Sub.none | |
| main = | |
| Browser.element | |
| { init = init | |
| , update = update | |
| , subscriptions = subscriptions | |
| , view = view | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Tests exposing (suite) | |
| import Expect exposing (Expectation) | |
| import Fuzz exposing (Fuzzer, int, list, string) | |
| import Main exposing (..) | |
| import Task exposing (Task) | |
| import Test exposing (..) | |
| suite : Test | |
| suite = | |
| describe "The Main module" | |
| [ describe "json2Foo" | |
| [ test "nothing must" <| | |
| \_ -> | |
| let | |
| json = | |
| """ | |
| { "key": "key1", "must": 2 } | |
| """ | |
| foo = | |
| json2OriginalFoo json | |
| expected = | |
| Ok <| OriginalFoo Nothing 2 | |
| in | |
| Expect.equal expected foo | |
| ] | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment