Skip to content

Instantly share code, notes, and snippets.

@radix
Created February 3, 2017 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radix/8ca065f1be9f64547970943df900b3f6 to your computer and use it in GitHub Desktop.
Save radix/8ca065f1be9f64547970943df900b3f6 to your computer and use it in GitHub Desktop.
Elm bug: `Cannot read property 'tag' of undefined`
module ElmBug exposing (..)
import Json.Decode as JD
import Json.Helpers as JH
import Dict
sumDecoder : String -> List (String, a) -> List (String, JD.Decoder a) -> JD.Decoder a
sumDecoder name nullaryList singleFieldList = JD.oneOf
[ JH.decodeSumUnaries name
(Dict.fromList nullaryList)
, JH.decodeSumObjectWithSingleField name
(Dict.fromList singleFieldList)
]
type Condition
= RecurringEffect Effect
| Dead
| Incapacitated
| AddDamageBuff Int
type Effect
= ApplyCondition ConditionDuration Condition
| Heal Int
| Damage Int
| MultiEffect (List Effect)
| GenerateEnergy Int
type ConditionDuration
= Interminate
| Duration Int
conditionDecoder = sumDecoder "Condition"
[ ("Dead", Dead)
, ("Incapacitated", Incapacitated)]
[ ("RecurringEffect", JD.map RecurringEffect lazyEffectDecoder)
, ("AddDamageBuff", JD.map AddDamageBuff JD.int)]
lazyEffectDecoder = JD.lazy (\_ -> effectDecoder)
effectDecoder =
JH.decodeSumObjectWithSingleField "Effect"
(Dict.fromList
[ ("Heal", JD.map Heal JD.int)
, ("Damage", JD.map Damage JD.int)
, ("GenerateEnergy", JD.map GenerateEnergy JD.int)
, ("ApplyCondition", JD.map2 ApplyCondition
(JD.index 0 conditionDurationDecoder)
(JD.index 1 conditionDecoder))
, ("MultiEffect", JD.map MultiEffect (JD.list lazyEffectDecoder))
])
conditionDurationDecoder = sumDecoder "ConditionDuration"
[("Interminate", Interminate)]
[("ConditionDuration", JD.map Duration JD.int)]
-- > JD.decodeString ElmBug.conditionDecoder "{\"RecurringEffect\": {\"GenerateEnergy\": 10}}"
-- TypeError: Cannot read property 'tag' of undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment