Ich versuche mit dem Quellcode von elmplayground zu spielen und ich versuche, eine Konfigurations-JSON-Datei für den Blog zu erstellen. Das Problem, das ich an diesem Punkt habe, ist, dass ich nicht weiß, wie ich einen Post-/Seitenautor als verschachtelte Struktur dekodieren kann. Was ich will ist, dass das author
Feld in Posts und Seiten einen Verweis auf einen Autor in der config.json
.Entschlüsseln verschachtelte JSON-Strukturen
config.json:
{
"posts": [{
"slug": "/hello-world",
"title": "Hello World",
"name": "hello-world",
"publishedDate": "2016-10-30",
"author": "Gabriel",
"intro": ""
}],
"pages": [{
"slug": "/hello",
"name": "index",
"title": "Elm Playground",
"publishedDate": "2016-09-01",
"author": "Gabriel",
"intro": ""
}],
"authors": {
"Gabriel": {
"name": "Gabriel Perales",
"avatar": "..."
}
}
}
Typ Inhalt für Seiten und Beiträge:
type alias Content =
{ title : String
, name : String
, slug : String
, publishedDate : Date
, author : Author
, markdown : WebData String
, contentType : ContentType
, intro : String
}
Typ Autor:
type alias Author =
{ name : String
, avatar : String
}
Derzeit ist dies meine Config-Decoder:
configDecoder : Decoder Config
configDecoder =
Decode.map2 Config
(field "posts" <| Decode.list <| decodeContent Post)
(field "pages" <| Decode.list <| decodeContent Page)
decodeContent : ContentType -> Decoder Content
decodeContent contentType =
Decode.map8 Content
(field "slug" string)
(field "name" string)
(field "title" string)
(field "publishedDate" decodeDate)
(field "author"
(string
-- I want to decode the author from "authors"
-- I have tried with
-- (\name -> at ["authors", name] decodeCofigAuthor) but it doesn\'t work
|> andThen (\name -> Decode.succeed <| Author name "...")
)
)
(Decode.succeed RemoteData.NotAsked)
(Decode.succeed contentType)
(field "intro" string)
decodeConfigAuthor : Decoder Author
decodeConfigAuthor =
Decode.map2 Author
(field "name" string)
(field "avatar" string)
Ich werde es versuchen, aber es sieht aus wie das, was ich gesucht habe – gabrielperales