diff options
author | Noah Loomans <noahloomans@gmail.com> | 2017-09-13 21:36:32 +0200 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2017-09-13 21:36:32 +0200 |
commit | 83034116b02497576d97bffc9e91a5476cc60d41 (patch) | |
tree | f0a89cf605e6613101cb31b7d9ae211ad37a8f70 | |
parent | e87bf34e06f8bec3c6622102528c2d685b3cbb5b (diff) |
decode union type
-rw-r--r-- | src/client/elm/Main.elm | 58 | ||||
-rw-r--r-- | src/client/static/elm.js | 44 |
2 files changed, 78 insertions, 24 deletions
diff --git a/src/client/elm/Main.elm b/src/client/elm/Main.elm index 8e1e9c8..de1f1fe 100644 --- a/src/client/elm/Main.elm +++ b/src/client/elm/Main.elm @@ -1,8 +1,7 @@ module Main exposing (..) --- import Html.Attributes exposing (..) import Html exposing (..) -import Json.Decode exposing (string, Decoder) +import Json.Decode exposing (Decoder, andThen, fail, string, succeed) import Json.Decode.Pipeline exposing (decode, required) @@ -15,7 +14,9 @@ main = , subscriptions = subscriptions } -type alias Flags = Json.Decode.Value + +type alias Flags = + Json.Decode.Value type alias Model = @@ -24,7 +25,7 @@ type alias Model = type alias User = - { type_ : String + { type_ : UserType , value : String } @@ -35,31 +36,62 @@ type UserType | Room | Student + init : Flags -> ( Model, Cmd msg ) init flags = case Json.Decode.decodeValue decodeUsers flags of Ok user -> - (Model user, Cmd.none) + ( Model user, Cmd.none ) Err err -> Debug.crash err + decodeUsers : Decoder (List User) -decodeUsers = Json.Decode.list decodeUser +decodeUsers = + Json.Decode.list decodeUser + decodeUser : Decoder User decodeUser = - decode User - |> required "type" string - |> required "value" string + decode User + |> required "type" decodeUserType + |> required "value" string + + +decodeUserType : Json.Decode.Decoder UserType +decodeUserType = + string + |> andThen + (\s -> + case s of + "s" -> + succeed Student + + "c" -> + succeed Class + + "t" -> + succeed Teacher + + "r" -> + succeed Room + + _ -> + fail ("What the f*ck is " ++ s ++ "?") + ) + + +update : msg -> Model -> ( Model, Cmd msg ) +update msg model = + ( model, Cmd.none ) -update : msg -> Model -> (Model, Cmd msg) -update msg model = (model, Cmd.none) view : Model -> Html msg view model = - text <| toString model + text <| toString model + subscriptions : Model -> Sub msg subscriptions model = - Sub.none
\ No newline at end of file + Sub.none diff --git a/src/client/static/elm.js b/src/client/static/elm.js index 1ac0ee4..cbab2b5 100644 --- a/src/client/static/elm.js +++ b/src/client/static/elm.js @@ -7893,6 +7893,32 @@ var _user$project$Main$User = F2( function (a, b) { return {type_: a, value: b}; }); +var _user$project$Main$Student = {ctor: 'Student'}; +var _user$project$Main$Room = {ctor: 'Room'}; +var _user$project$Main$Teacher = {ctor: 'Teacher'}; +var _user$project$Main$Class = {ctor: 'Class'}; +var _user$project$Main$decodeUserType = A2( + _elm_lang$core$Json_Decode$andThen, + function (s) { + var _p0 = s; + switch (_p0) { + case 's': + return _elm_lang$core$Json_Decode$succeed(_user$project$Main$Student); + case 'c': + return _elm_lang$core$Json_Decode$succeed(_user$project$Main$Class); + case 't': + return _elm_lang$core$Json_Decode$succeed(_user$project$Main$Teacher); + case 'r': + return _elm_lang$core$Json_Decode$succeed(_user$project$Main$Room); + default: + return _elm_lang$core$Json_Decode$fail( + A2( + _elm_lang$core$Basics_ops['++'], + 'What the f*ck is ', + A2(_elm_lang$core$Basics_ops['++'], s, '?'))); + } + }, + _elm_lang$core$Json_Decode$string); var _user$project$Main$decodeUser = A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, 'value', @@ -7900,33 +7926,29 @@ var _user$project$Main$decodeUser = A3( A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, 'type', - _elm_lang$core$Json_Decode$string, + _user$project$Main$decodeUserType, _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$decode(_user$project$Main$User))); var _user$project$Main$decodeUsers = _elm_lang$core$Json_Decode$list(_user$project$Main$decodeUser); var _user$project$Main$init = function (flags) { - var _p0 = A2(_elm_lang$core$Json_Decode$decodeValue, _user$project$Main$decodeUsers, flags); - if (_p0.ctor === 'Ok') { + var _p1 = A2(_elm_lang$core$Json_Decode$decodeValue, _user$project$Main$decodeUsers, flags); + if (_p1.ctor === 'Ok') { return { ctor: '_Tuple2', - _0: _user$project$Main$Model(_p0._0), + _0: _user$project$Main$Model(_p1._0), _1: _elm_lang$core$Platform_Cmd$none }; } else { return _elm_lang$core$Native_Utils.crashCase( 'Main', { - start: {line: 40, column: 5}, - end: {line: 45, column: 28} + start: {line: 44, column: 5}, + end: {line: 49, column: 28} }, - _p0)(_p0._0); + _p1)(_p1._0); } }; var _user$project$Main$main = _elm_lang$html$Html$programWithFlags( {init: _user$project$Main$init, update: _user$project$Main$update, view: _user$project$Main$view, subscriptions: _user$project$Main$subscriptions})(_elm_lang$core$Json_Decode$value); -var _user$project$Main$Student = {ctor: 'Student'}; -var _user$project$Main$Room = {ctor: 'Room'}; -var _user$project$Main$Teacher = {ctor: 'Teacher'}; -var _user$project$Main$Class = {ctor: 'Class'}; var Elm = {}; Elm['Main'] = Elm['Main'] || {}; |