-
Notifications
You must be signed in to change notification settings - Fork 45
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
Support for Tuple #20
Closed
Closed
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
51142b3
Support for Tuple
jacereda 8aba8bf
Added another tuple test
jacereda 49139b9
Simplify asTuple with @kRITZCREEK's suggestion
jacereda fe03fe7
Fix nested tuples and n-tuples
jacereda 341cffe
Write unit as empty record
jacereda 3161287
Leave WriteForeign Unit as it was
jacereda 9f22a07
Warnings
jacereda e0b8e67
Get rid of unit reading/writing
jacereda 303f40f
Compromise solution, no overlapped instances, no support for Data.Tup…
jacereda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,9 @@ module Simple.JSON ( | |
import Prelude | ||
|
||
import Control.Monad.Except (runExcept, withExcept) | ||
import Data.Array (length, unsafeIndex) | ||
import Data.Either (Either) | ||
import Data.Foreign (F, Foreign, ForeignError(..), MultipleErrors, readArray, readBoolean, readChar, readInt, readNull, readNumber, readString, toForeign) | ||
import Data.Foreign (F, Foreign, ForeignError(..), MultipleErrors, fail, readArray, readBoolean, readChar, readInt, readNull, readNumber, readString, toForeign) | ||
import Data.Foreign.Index (readProp) | ||
import Data.Foreign.Internal (readStrMap) | ||
import Data.Foreign.JSON (parseJSON) | ||
|
@@ -35,7 +36,9 @@ import Data.Record.Builder as Builder | |
import Data.StrMap as StrMap | ||
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol) | ||
import Data.Traversable (sequence, traverse) | ||
import Data.Tuple (Tuple(..)) | ||
import Global.Unsafe (unsafeStringify) | ||
import Partial.Unsafe (unsafePartial) | ||
import Type.Row (class RowLacks, class RowToList, Cons, Nil, RLProxy(RLProxy), kind RowList) | ||
|
||
-- | Read a JSON string to a type `a` while returning a `MultipleErrors` if the | ||
|
@@ -76,47 +79,58 @@ read = readImpl | |
class ReadForeign a where | ||
readImpl :: Foreign -> F a | ||
|
||
instance readForeign :: ReadForeign Foreign where | ||
instance readForeignForeign :: ReadForeign Foreign where | ||
readImpl = pure | ||
|
||
instance readChar :: ReadForeign Char where | ||
instance readForeignChar :: ReadForeign Char where | ||
readImpl = readChar | ||
|
||
instance readNumber :: ReadForeign Number where | ||
instance readForeignNumber :: ReadForeign Number where | ||
readImpl = readNumber | ||
|
||
instance readInt :: ReadForeign Int where | ||
instance readForeignInt :: ReadForeign Int where | ||
readImpl = readInt | ||
|
||
instance readString :: ReadForeign String where | ||
instance readForeignString :: ReadForeign String where | ||
readImpl = readString | ||
|
||
instance readBoolean :: ReadForeign Boolean where | ||
instance readForeignBoolean :: ReadForeign Boolean where | ||
readImpl = readBoolean | ||
|
||
instance readArray :: ReadForeign a => ReadForeign (Array a) where | ||
instance readForeignArray :: ReadForeign a => ReadForeign (Array a) where | ||
readImpl = readElements <=< readArray | ||
where | ||
readElements xs = sequence $ readImpl <$> xs | ||
|
||
instance readNullOrUndefined :: ReadForeign a => ReadForeign (NullOrUndefined a) where | ||
instance readForeignNullOrUndefined :: ReadForeign a => ReadForeign (NullOrUndefined a) where | ||
readImpl = readNullOrUndefined readImpl | ||
|
||
instance readMaybe :: ReadForeign a => ReadForeign (Maybe a) where | ||
instance readForeignMaybe :: ReadForeign a => ReadForeign (Maybe a) where | ||
readImpl = map unNullOrUndefined <<< readImpl | ||
|
||
instance readNullable :: ReadForeign a => ReadForeign (Nullable a) where | ||
instance readForeignNullable :: ReadForeign a => ReadForeign (Nullable a) where | ||
readImpl o = withExcept (map reformat) $ | ||
map toNullable <$> traverse readImpl =<< readNull o | ||
where | ||
reformat error = case error of | ||
TypeMismatch inner other -> TypeMismatch ("Nullable " <> inner) other | ||
_ -> error | ||
|
||
instance readStrMap :: ReadForeign a => ReadForeign (StrMap.StrMap a) where | ||
instance readForeignStrMap :: ReadForeign a => ReadForeign (StrMap.StrMap a) where | ||
readImpl = sequence <<< StrMap.mapWithKey (const readImpl) <=< readStrMap | ||
|
||
instance readRecord :: | ||
instance readForeignTuple :: (ReadForeign a, ReadForeign b) => ReadForeign (Tuple a b) where | ||
readImpl = asTuple <=< readArray | ||
where asTuple :: Array Foreign -> F (Tuple a b) | ||
asTuple arr = case length arr of | ||
2 -> do | ||
let get a i = unsafePartial $ unsafeIndex a i | ||
a <- readImpl $ get arr 0 | ||
b <- readImpl $ get arr 1 | ||
pure $ Tuple a b | ||
l -> fail $ TypeMismatch "2 values" (show l <> " values") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't this provide more information?.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would you suggest? |
||
|
||
instance readForeignRecord :: | ||
( RowToList fields fieldList | ||
, ReadForeignFields fieldList () fields | ||
) => ReadForeign (Record fields) where | ||
|
@@ -133,7 +147,7 @@ class ReadForeignFields (xs :: RowList) (from :: # Type) (to :: # Type) | |
-> Foreign | ||
-> F (Builder (Record from) (Record to)) | ||
|
||
instance readFieldsCons :: | ||
instance readForeignFieldsCons :: | ||
( IsSymbol name | ||
, ReadForeign ty | ||
, ReadForeignFields tail from from' | ||
|
@@ -153,7 +167,7 @@ instance readFieldsCons :: | |
name = reflectSymbol nameP | ||
withExcept' = withExcept <<< map $ ErrorAtProperty name | ||
|
||
instance readFieldsNil :: | ||
instance readForeignFieldsNil :: | ||
ReadForeignFields Nil () () where | ||
getFields _ _ = | ||
pure id | ||
|
@@ -197,6 +211,9 @@ instance writeForeignNullable :: WriteForeign a => WriteForeign (Nullable a) whe | |
instance writeForeignStrMap :: WriteForeign a => WriteForeign (StrMap.StrMap a) where | ||
writeImpl = toForeign <<< StrMap.mapWithKey (const writeImpl) | ||
|
||
instance writeForeignTuple :: (WriteForeign a, WriteForeign b) => WriteForeign (Tuple a b) where | ||
writeImpl (Tuple a b) = writeImpl [toForeign a, toForeign b] | ||
|
||
instance recordWriteForeign :: | ||
( RowToList row rl | ||
, WriteForeignFields rl row () to | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this code would be simpler by pattern matching on arr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course, thanks.