New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding a Maybe-value #8

Closed
k-bx opened this Issue Nov 4, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@k-bx

k-bx commented Nov 4, 2018

Hi

I'm using elm-bridge to generate decoders and encoders with Elm 0.19 and json-helpers 2.0.0.

Maybe-value parsing is failing for me. I can show the full code that's generated from elm-bridge, but here I've recreated the essence:

module Main exposing (main)                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                     
import Browser                                                                                                                                                                                                                                                                                                       
import Html exposing (..)                                                                                                                                                                                                                                                                                            
import Json.Decode as Decode                                                                                                                                                                                                                                                                                         
import Json.Helpers as JsonHelpers                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                     
type alias Model =                                                                                                                                                                                                                                                                                                   
    ()                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                     
type alias Msg =                                                                                                                                                                                                                                                                                                     
    ()                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                     
type alias Foo =                                                                                                                                                                                                                                                                                                     
    { foo : Maybe Int }                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                     
view _ =                                                                                                                                                                                                                                                                                                             
    let                                                                                                                                                                                                                                                                                                              
        jsonDecFoo : Decode.Decoder Foo                                                                                                                                                                                                                                                                              
        jsonDecFoo =                                                                                                                                                                                                                                                                                                 
            Decode.succeed (\pfoo -> { foo = pfoo })                                                                                                                                                                                                                                                                 
                |> JsonHelpers.fnullable "foo" Decode.int                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                     
        decodeResult =                                                                                                                                                                                                                                                                                               
            Decode.decodeString jsonDecFoo "{\"foo\": null}"                                                                                                                                                                                                                                                         
    in                                                                                                                                                                                                                                                                                                               
    case decodeResult of                                                                                                                                                                                                                                                                                             
        Ok val ->                                                                                                                                                                                                                                                                                                    
            text "ok"                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                     
        Err err ->                                                                                                                                                                                                                                                                                                   
            text ("not ok: " ++ Decode.errorToString err)                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                     
main : Program () Model Msg                                                                                                                                                                                                                                                                                          
main =                                                                                                                                                                                                                                                                                                               
    Browser.sandbox                                                                                                                                                                                                                                                                                                  
        { init = ()                                                                                                                                                                                                                                                                                                  
        , view = view                                                                                                                                                                                                                                                                                                
        , update = \x y -> y                                                                                                                                                                                                                                                                                         
        }                                                                                                                                                                                                                                                                                                            

Full repo which can be built is at https://github.com/k-bx/json-helpers-nullable

The error I get is:

not ok: Json.Decode.oneOf failed in the following 2 ways: (1) Problem with the given value: { "foo": null } Expecting null (2) Problem with the value at json.foo: null Expecting an INT

Help appreciated!

@k-bx k-bx referenced this issue Nov 4, 2018

Closed

Fix fnullable #9

@bartavelle bartavelle closed this in 550f45d Nov 6, 2018

@bartavelle

This comment has been minimized.

Owner

bartavelle commented Nov 6, 2018

reopen, there is a bug in elm-bridge

@k-bx

This comment has been minimized.

k-bx commented Nov 6, 2018

Hmm, there's no button to reopen. Please make a new issue if needed! (as I'm not sure what's the bug exactly)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment