I’m struggling to wrap my head around this, grateful for any help.
First off, I want to populate this type:
type alias Entity =
{ name : String
, plural : String
}
Scenario 1: plural explicitly specified:
run parseEntity "Entity Sheep plural Sheep" == Ok {name = "Sheep", plural = "Sheep"}
Scenario 2: plural not specified, derived by adding ‘s’ to name:
run parseEntity "Entity Dog" == Ok {name = "Dog", plural = "Dogs"}
A parser function for scenario 1 only is straightforward:
parseEntity : Parser Entity
parseEntity =
succeed Entity
|. keyword "Entity"
|. spaces
|= parseVarName
|. spaces
|. keyword "plural"
|. spaces
|= parseVarName
where parseVarName
just wraps the variable
function & returns a string. I’m struggling to figure out how to extend this to cover scenario 2. Intuitively, I can see that oneOf
is likely part of the answer - and possibly map
to set the default? Putting them together proving elusive however.
Thanks for any help.