medeamelana / jsongrammar2 Goto Github PK
View Code? Open in Web Editor NEWLicense: Other
License: Other
Right now there is hardly any Haddock documentation on the various functions and combinators. We need to write some before we can publish the new version of JsonGrammar on hackage.
Hello again :)
I tried my best to copy/paste the example from the documentation in order to run it:
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text as T
import Language.JsonGrammar
import Data.StackPrism.TH -- (StackPrism, deriveStackPrismsFor)
data Person = Person
{ name :: T.Text
, age :: Int
} deriving Show
deriveStackPrismsFor ["person"] ''Person
instance Json Person where
grammar = fromPrism person . object (prop "name" . prop "age")
But this results in the following compile-time errors (line 24 is the "grammar = " definition):
/Users/rune/IdeaProjects/bitcoin-payment-channel-example/src/Common/Types.hs:24:13:
Couldn't match expected type ‘b0 -> c0’
with actual type ‘Grammar
c1 (T.Text :- (Int :- t2)) (Person :- t2)’
Possible cause: ‘fromPrism’ is applied to too many arguments
In the first argument of ‘(.)’, namely ‘fromPrism person’
In the expression:
fromPrism person . object (prop "name" . prop "age")
/Users/rune/IdeaProjects/bitcoin-payment-channel-example/src/Common/Types.hs:24:13:
Couldn't match expected type ‘Grammar
'Val
(aeson-0.9.0.1:Data.Aeson.Types.Internal.Value :- t)
(Person :- t)’
with actual type ‘a0 -> c0’
Relevant bindings include
grammar :: Grammar
'Val
(aeson-0.9.0.1:Data.Aeson.Types.Internal.Value :- t)
(Person :- t)
(bound at src/Common/Types.hs:24:3)
In the expression:
fromPrism person . object (prop "name" . prop "age")
In an equation for ‘grammar’:
grammar = fromPrism person . object (prop "name" . prop "age")
In the instance declaration for ‘Json Person’
/Users/rune/IdeaProjects/bitcoin-payment-channel-example/src/Common/Types.hs:24:32:
Couldn't match expected type ‘a0 -> b0’
with actual type ‘Grammar
'Val (aeson-0.9.0.1:Data.Aeson.Types.Internal.Value :- t10) t20’
Possible cause: ‘object’ is applied to too many arguments
In the second argument of ‘(.)’, namely
‘object (prop "name" . prop "age")’
In the expression:
fromPrism person . object (prop "name" . prop "age")
/Users/rune/IdeaProjects/bitcoin-payment-channel-example/src/Common/Types.hs:24:40:
Couldn't match expected type ‘b1 -> c2’
with actual type ‘Grammar 'Obj t0 (a2 :- t0)’
Possible cause: ‘prop’ is applied to too many arguments
In the first argument of ‘(.)’, namely ‘prop "name"’
In the first argument of ‘object’, namely
‘(prop "name" . prop "age")’
/Users/rune/IdeaProjects/bitcoin-payment-channel-example/src/Common/Types.hs:24:40:
Couldn't match expected type ‘Grammar 'Obj t10 t20’
with actual type ‘a1 -> c2’
In the first argument of ‘object’, namely
‘(prop "name" . prop "age")’
In the second argument of ‘(.)’, namely
‘object (prop "name" . prop "age")’
In the expression:
fromPrism person . object (prop "name" . prop "age")
/Users/rune/IdeaProjects/bitcoin-payment-channel-example/src/Common/Types.hs:24:54:
Couldn't match expected type ‘a1 -> b1’
with actual type ‘Grammar 'Obj t1 (a3 :- t1)’
Possible cause: ‘prop’ is applied to too many arguments
In the second argument of ‘(.)’, namely ‘prop "age"’
In the first argument of ‘object’, namely
‘(prop "name" . prop "age")’
Please bump up the aeson bound to support the current aeson-0.9.0.1.
If I have
data RequestSessionUpdate =
RequestUpdateSourceFileFromFile RequestFilePath
| RequestUpdateGhcOpts RequestGhcOption
with corresponding grammar
instance Json RequestSessionUpdate where
grammar = label "SessionUpdate" $
object $ mconcat [
property "sessionUpdate" "updateSourceFileFromFile"
. fromPrism requestUpdateSourceFileFromFile . prop "filePath"
, property "sessionUpdate" "updateGhcOpts"
. fromPrism requestUpdateGhcOpts . prop "options"
]
the documentation looks like
interface SessionUpdate {filePath ? : FilePath ;sessionUpdate ? : string ;options ? : GhcOption ;}
This is not ideal (see #5), but at least it lists all fields. However, once I change the type to use a list
data RequestSessionUpdate =
RequestUpdateSourceFileFromFile RequestFilePath
| RequestUpdateGhcOpts [RequestGhcOption]
the documentation suddenly doesn't list the options
field at all anymore:
interface SessionUpdate {filePath ? : FilePath ;sessionUpdate ? : string ;}
aeson has several benchmarks to measure its performance on files of various types and sizes. I'm very interested in how an aeson parser expressed as a JsonGrammar performs in these tests. Ideally they convert to the same low-level code and have identical performance, but I don't know what we need to do to make that work.
Instead of depending on package Piso for partial isomorphisms, we should explore the option of depending on lenses, specifically prisms, for wrapping and unwrapping constructors.
Right now there is function fromPiso
that converts a Piso
to a Grammar
(specifically a Pure
node); perhaps we can write a function fromPrism
that converts a prism to a Pure
node.
It'd be awesome if we could build a Grammar
building block to express TypeScript string/number index signatures in such a way that interfaces
actually generates such index signatures. For example:
interface IdList {
order: number[];
x: number]: any;
}
I don't know what such a building block should look like so there is some designing to do there.
Usually an error message is just "empty grammar". This might not be such an easy problem to tackle though.
Hi
First of all, thank you for creating this library. I had been writing JSON instances by hand for a while, always feeling like I was doing redundant work, so I'm really glad I've found this.
When I try to build my project using the Stack lts-5.2 snapshot, I get the following error:
While constructing the BuildPlan the following exceptions were encountered:
-- Failure when adding dependencies:
semigroups: needed (>=0.5 && <0.17), 0.18.1 found (latest applicable is 0.16.2.2)
stack-prism: needed (>=0.1 && <0.2), couldn't resolve its dependencies
needed for package: JsonGrammar-1.0.3
-- Failure when adding dependencies:
profunctors: needed (>=4.0 && <5.2), 5.2 found (latest applicable is 5.1.2)
needed for package: stack-prism-0.1.4
Would it be possible to update JsonGrammar
and stack-prism
to work with the newer versions semigroups
and profunctors
, respectively?
If you have a definition such as
instance Json RequestSessionUpdate where
grammar = label "SessionUpdate" $
object $ mconcat [
property "sessionUpdate" "updateSourceFileFromFile"
. fromPrism requestUpdateSourceFileFromFile . prop "filePath"
, property "sessionUpdate" "updateGhcOpts"
. fromPrism requestUpdateGhcOpts . prop "options"
]
(following the example use of property
rather closely), then the API docs look like
interface SessionUpdate {filePath ? : FilePath ;sessionUpdate ? : string ;options ? : string ;}
But this is not particularly informative; it loses the fact that we want the filePath
in one case, and the options
in another, and it doesn't tell us what values sessionUpdate
is expected to have.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.