2016-06-23 17 views
7

Wpadłem na problem podczas wypróbowania Wiązu. Chcę przekazać typ Unii przez port ale otrzymuję ten błąd:Jak przekazywać typy związków przez porty Wiązów?

Port `setStorage` is trying to communicate an unsupported type. 

34| port setStorage : Model -> Cmd msg 
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
The specific unsupported type is: 

    Todo.Importance 

The types of values that can flow through in and out of Elm include: 

    Ints, Floats, Bools, Strings, Maybes, Lists, Arrays, 
    Tuples, Json.Values, and concrete records. 

Mam zmodyfikowany TODO example następująco:

type alias Task = 
    { description : String 
    , completed : Bool 
    , editing : Bool 
    , id : Int 
    , importance : Importance 
    } 

type Importance 
    = Normal 
    | High 
    | Low 

Ten issue wydaje się być dość stary. Jeden z komentujących sugeruje, aby "przekazać Json.Values ​​przez porty i Json.Decode/Encode ich", ale jak to dokładnie zrobić? Dokumentacja wydaje się nieco niejasna i nie ma pełnych przykładów. Każda pomoc doceniona.

Odpowiedz

7

Zrobiłem to work z Json.Decoder/Encoder. Wcale nie było to trudne, chociaż konieczność serializowania każdego pojedynczego pola tylko po to, by przekazać ten typ unii, jest ciężarem.

Dekodery:

modelDecoder : Json.Decoder Model 
modelDecoder = 
    Json.object4 Model 
    ("tasks" := Json.list taskDecoder) 
    ("field" := Json.string) 
    ("uid" := Json.int) 
    ("visibility" := Json.string) 

taskDecoder : Json.Decoder Task 
taskDecoder = 
    Json.object5 Task 
    ("description" := Json.string) 
    ("completed" := Json.bool) 
    ("editing" := Json.bool) 
    ("id" := Json.int) 
    ("importance" := Json.string `andThen` importanceDecoder) 

importanceDecoder : String -> Json.Decoder Importance 
importanceDecoder tag = 
    case tag of 
    "Normal" -> Json.succeed Normal 
    "High" -> Json.succeed High 
    "Low" -> Json.succeed Low 
    _ -> Json.fail (tag ++ " is not a recognized tag for Importance") 

i koderów:

modelToValue : Model -> Json.Encode.Value 
modelToValue model = 
    Json.Encode.object 
    [ 
     ("tasks", Json.Encode.list (List.map taskToValue model.tasks)), 
     ("field", Json.Encode.string model.field), 
     ("uid", Json.Encode.int model.uid), 
     ("visibility", Json.Encode.string model.visibility) 
    ] 

taskToValue : Task -> Json.Encode.Value 
taskToValue task = 
    Json.Encode.object 
    [ 
     ("description", Json.Encode.string task.description), 
     ("completed", Json.Encode.bool task.completed), 
     ("editing", Json.Encode.bool task.editing), 
     ("id", Json.Encode.int task.id), 
     ("importance", importanceToValue task.importance) 
    ] 

importanceToValue : Importance -> Json.Encode.Value 
importanceToValue importance = 
    case importance of 
    Normal -> Json.Encode.string "Normal" 
    High -> Json.Encode.string "High" 
    Low -> Json.Encode.string "Low" 
+1

Ta funkcja nie będą realizowane w najbliższym czasie ... nad wiązów Slack @rtfeldman mówi ta funkcja jest droga w dół na liście priorytetów i że byłby zaskoczony, gdyby stało się to w przyszłym roku. To było od 23 maja 2017 r. –

3

Nie można przekazać typu unii przede wszystkim, ponieważ JS nie wie o czymś takim. Więc równie dobrze możesz przekazać ciąg znaków i wykonać instrukcję case w javascript - robię to cały czas.

Powiązane problemy