fix: write/read foreign impls

This commit is contained in:
orion 2023-12-25 23:37:22 -06:00
parent 57dc73d20b
commit 6faac5b5c5
Signed by: orion
GPG Key ID: 6D4165AE4C928719
3 changed files with 103 additions and 0 deletions

View File

@ -4,8 +4,11 @@ workspace:
path: ./ path: ./
dependencies: dependencies:
- arrays - arrays
- bifunctors
- either
- filterable - filterable
- foldable-traversable - foldable-traversable
- foreign
- integers - integers
- maybe - maybe
- newtype - newtype
@ -13,7 +16,9 @@ workspace:
- ordered-collections - ordered-collections
- partial - partial
- prelude - prelude
- simple-json
- strings - strings
- transformers
- tuples - tuples
test_dependencies: test_dependencies:
- aff - aff
@ -45,6 +50,8 @@ workspace:
- exists - exists
- filterable - filterable
- foldable-traversable - foldable-traversable
- foreign
- foreign-object
- fork - fork
- free - free
- functions - functions
@ -69,8 +76,10 @@ workspace:
- pipes - pipes
- prelude - prelude
- profunctor - profunctor
- record
- refs - refs
- safe-coerce - safe-coerce
- simple-json
- spec - spec
- st - st
- strings - strings
@ -78,8 +87,10 @@ workspace:
- transformers - transformers
- tuples - tuples
- type-equality - type-equality
- typelevel-prelude
- unfoldable - unfoldable
- unsafe-coerce - unsafe-coerce
- variant
package_set: package_set:
address: address:
hash: sha256-nTsd44o7/hrTdk0c6dh0wyBqhFFDJJIeKdQU6L1zv/A= hash: sha256-nTsd44o7/hrTdk0c6dh0wyBqhFFDJJIeKdQU6L1zv/A=
@ -5916,6 +5927,37 @@ packages:
- orders - orders
- prelude - prelude
- tuples - tuples
foreign:
type: git
url: https://github.com/purescript/purescript-foreign.git
rev: 2dd222d1ec7363fa0a0a7adb0d8eaf81bb7006dd
dependencies:
- either
- functions
- identity
- integers
- lists
- maybe
- prelude
- strings
- transformers
foreign-object:
type: git
url: https://github.com/purescript/purescript-foreign-object.git
rev: 9bfb4eb6271b151414594cfec669fb4b18b91bd1
dependencies:
- arrays
- foldable-traversable
- functions
- gen
- lists
- maybe
- prelude
- st
- tailrec
- tuples
- typelevel-prelude
- unfoldable
fork: fork:
type: git type: git
url: https://github.com/purescript-contrib/purescript-fork.git url: https://github.com/purescript-contrib/purescript-fork.git
@ -6161,6 +6203,14 @@ packages:
- newtype - newtype
- prelude - prelude
- tuples - tuples
record:
type: git
url: https://github.com/purescript/purescript-record.git
rev: c89cd1ada6b636692571fc374196b1c39c4c9f70
dependencies:
- functions
- prelude
- unsafe-coerce
refs: refs:
type: git type: git
url: https://github.com/purescript/purescript-refs.git url: https://github.com/purescript/purescript-refs.git
@ -6174,6 +6224,20 @@ packages:
rev: 7fa799ae80a38b8d948efcb52608e58e198b3da7 rev: 7fa799ae80a38b8d948efcb52608e58e198b3da7
dependencies: dependencies:
- unsafe-coerce - unsafe-coerce
simple-json:
type: git
url: https://github.com/justinwoo/purescript-simple-json.git
rev: b85e112131240ff95b5c26e9abb8e2fa6db3c656
dependencies:
- arrays
- exceptions
- foreign
- foreign-object
- nullable
- prelude
- record
- typelevel-prelude
- variant
spec: spec:
type: git type: git
url: https://github.com/purescript-spec/purescript-spec.git url: https://github.com/purescript-spec/purescript-spec.git
@ -6282,6 +6346,13 @@ packages:
url: https://github.com/purescript/purescript-type-equality.git url: https://github.com/purescript/purescript-type-equality.git
rev: 0525b7d39e0fbd81b4209518139fb8ab02695774 rev: 0525b7d39e0fbd81b4209518139fb8ab02695774
dependencies: [] dependencies: []
typelevel-prelude:
type: git
url: https://github.com/purescript/purescript-typelevel-prelude.git
rev: dca2fe3c8cfd5527d4fe70c4bedfda30148405bf
dependencies:
- prelude
- type-equality
unfoldable: unfoldable:
type: git type: git
url: https://github.com/purescript/purescript-unfoldable.git url: https://github.com/purescript/purescript-unfoldable.git
@ -6297,3 +6368,16 @@ packages:
url: https://github.com/purescript/purescript-unsafe-coerce.git url: https://github.com/purescript/purescript-unsafe-coerce.git
rev: ab956f82e66e633f647fb3098e8ddd3ec58d689f rev: ab956f82e66e633f647fb3098e8ddd3ec58d689f
dependencies: [] dependencies: []
variant:
type: git
url: https://github.com/natefaubion/purescript-variant.git
rev: 8b2bd73a4202257d80a955c4dd36f6055f149823
dependencies:
- enums
- lists
- maybe
- partial
- prelude
- record
- tuples
- unsafe-coerce

View File

@ -17,8 +17,11 @@ package:
- tuples - tuples
dependencies: dependencies:
- arrays - arrays
- bifunctors
- either
- filterable - filterable
- foldable-traversable - foldable-traversable
- foreign
- integers - integers
- maybe - maybe
- newtype - newtype
@ -26,7 +29,9 @@ package:
- ordered-collections - ordered-collections
- partial - partial
- prelude - prelude
- simple-json
- strings - strings
- transformers
- tuples - tuples
workspace: workspace:
extra_packages: {} extra_packages: {}

View File

@ -35,7 +35,10 @@ module Data.URL
import Prelude import Prelude
import Control.Monad.Error.Class (liftEither, liftMaybe)
import Data.Array as Array import Data.Array as Array
import Data.Bifunctor (lmap)
import Data.Either (Either)
import Data.Filterable (filter) import Data.Filterable (filter)
import Data.Foldable (class Foldable, foldl, intercalate) import Data.Foldable (class Foldable, foldl, intercalate)
import Data.FoldableWithIndex (foldlWithIndex) import Data.FoldableWithIndex (foldlWithIndex)
@ -50,7 +53,9 @@ import Data.Nullable as Nullable
import Data.Show.Generic (genericShow) import Data.Show.Generic (genericShow)
import Data.String as String import Data.String as String
import Data.Tuple.Nested (type (/\), (/\)) import Data.Tuple.Nested (type (/\), (/\))
import Foreign (ForeignError(..))
import Partial.Unsafe (unsafePartial) import Partial.Unsafe (unsafePartial)
import Simple.JSON (class ReadForeign, class WriteForeign, readImpl, writeImpl)
class QueryParam a where class QueryParam a where
queryParamTuple :: a -> String /\ Array String queryParamTuple :: a -> String /\ Array String
@ -64,6 +69,12 @@ else instance QueryParam (String /\ Array String) where
foreign import data URL :: Type foreign import data URL :: Type
instance WriteForeign URL where
writeImpl = writeImpl <<< toString
instance ReadForeign URL where
readImpl = flip bind (liftEither <<< lmap pure <<< lmap ForeignError <<< parse) <<< readImpl
instance Show URL where instance Show URL where
show u = "(URL " <> show (toString u) <> ")" show u = "(URL " <> show (toString u) <> ")"
@ -123,6 +134,9 @@ foreign import setUsernameImpl :: String -> URL -> URL
fromString :: String -> Maybe URL fromString :: String -> Maybe URL
fromString = Nullable.toMaybe <<< fromStringImpl fromString = Nullable.toMaybe <<< fromStringImpl
parse :: String -> Either String URL
parse url = liftMaybe ("invalid URL: " <> url) $ Nullable.toMaybe $ fromStringImpl url
toString :: URL -> String toString :: URL -> String
toString = hrefImpl toString = hrefImpl