-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Handle new struct property * Update README * Update schemas * Require newer `data-default-class` * Fix dictionary schema * Fix dictionary schema again
- Loading branch information
Showing
12 changed files
with
113 additions
and
54 deletions.
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
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
packages: . | ||
constraints: | ||
-- https://github.com/snoyberg/http-client/issues/547 | ||
data-default-class >= 0.2 |
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
Binary file not shown.
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module Rattletrap.Type.Property.Struct where | ||
|
||
import qualified Rattletrap.ByteGet as ByteGet | ||
import qualified Rattletrap.BytePut as BytePut | ||
import qualified Rattletrap.Schema as Schema | ||
import qualified Rattletrap.Type.Dictionary as Dictionary | ||
import qualified Rattletrap.Type.Str as Str | ||
import qualified Rattletrap.Utility.Json as Json | ||
|
||
data Struct a = Struct | ||
{ name :: Str.Str, | ||
fields :: Dictionary.Dictionary a | ||
} | ||
deriving (Eq, Show) | ||
|
||
instance (Json.FromJSON a) => Json.FromJSON (Struct a) where | ||
parseJSON = Json.withObject "Struct" $ \o -> do | ||
name <- Json.required o "name" | ||
fields <- Json.required o "fields" | ||
pure Struct {name, fields} | ||
|
||
instance (Json.ToJSON a) => Json.ToJSON (Struct a) where | ||
toJSON x = | ||
Json.object | ||
[ Json.pair "name" $ name x, | ||
Json.pair "fields" $ fields x | ||
] | ||
|
||
schema :: Schema.Schema -> Schema.Schema | ||
schema s = | ||
Schema.named "property-struct" $ | ||
Schema.object | ||
[ (Json.pair "name" $ Schema.ref Str.schema, True), | ||
(Json.pair "fields" $ Schema.ref (Dictionary.schema s), True) | ||
] | ||
|
||
bytePut :: (a -> BytePut.BytePut) -> Struct a -> BytePut.BytePut | ||
bytePut p x = | ||
Str.bytePut (name x) | ||
<> Dictionary.bytePut p (fields x) | ||
|
||
byteGet :: ByteGet.ByteGet a -> ByteGet.ByteGet (Struct a) | ||
byteGet g = ByteGet.label "Struct" $ do | ||
name <- ByteGet.label "name" Str.byteGet | ||
fields <- ByteGet.label "fields" $ Dictionary.byteGet g | ||
pure Struct {name, fields} |
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
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
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