How to make uppercase records in purescript?

Viewed 58

I have an API that forces me to return a JSON in the form of

{ Field1: "something"
, Field2: 12 }

However, I have failed to model this in Purescript so far.

I understand that the purescript grammar sees the upper case field names and thinks it is the type of a field without a field name. So I it is not straight forward to just encode a function like

test :: Number -> { Field1 :: String, Field2 :: Number }

Without resorting to using external javascript functions to change the object into something else, is it at all possible to construct a record with upper case field names in purescript? And if so, how?

1 Answers

Absolutely! PureScript allows any field names, not just "valid identifier" ones. You just have to double-quote them if they're weird:

x :: { "Field1" :: String, "Field2" :: String, "" :: String }
x = { "Field1": "foo", "Field2": "bar", "": "yum" }

y :: String
y = x.""

Basically same deal as in JavaScript.

Related