Skip to content

Parsing Foreign Records fails #52

Description

@Rembane

Good morning,

I have a record in a Foreign object, and when I try to turn it into a Purescript record using read' I get the following error:

Error at property "channel": Type mismatch: expected String, found Undefined

The interesting thing is that it just works if I first convert the Foreign object to a string.

Why does it behave like this?

Here's a quite large minimum failing example:

Main.js:

exports.rcrd = "{\"channel\":\"Hej där!\",\"glorp\":27,\"event\":\"mystisk uppenbarelse\",\"payload\":\"Just det...\"}";

Main.purs:

module Main where

import Prelude

import Control.Monad.Except (runExcept)
import Data.Either (Either(..))
import Data.Foldable (foldMap)
import Effect (Effect)
import Effect.Console (log, logShow)
import Foreign (Foreign, readString, renderForeignError, unsafeFromForeign)
import Simple.JSON (read', readJSON')

foreign import rcrd :: Foreign

iPrintRecords :: { channel :: String, glorp :: Int, event :: String, payload :: Foreign } -> Effect Unit
iPrintRecords r = log r.channel *> logShow r.glorp

main :: Effect Unit
main = do
  log "Hi! :D"
  log $ unsafeFromForeign rcrd
  log "----------------------------------------------------------------------------------------"
  log "This fails:"
  case runExcept (read' rcrd) of
    Left es -> log (foldMap renderForeignError es)
    Right r -> iPrintRecords r

  log "\nThis works:"
  case runExcept (readJSON' =<< readString rcrd) of
    Left es -> log (foldMap renderForeignError es)
    Right r -> iPrintRecords r

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions