-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add hidden field #5
Conversation
Hi Cristhian, thanks for your patch! I am reviewing this 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.
Very impressive work!
Please see my comments in the code. Also:
- You might change the
evalObj
method to account for hidden fields, then in theVObj
constructor change the keys fromText
to a new type, e.g.data Key = Visible Text | Hidden Text
the idea here is that fields should all be usable, the only time we care about whether it's a hidden field is when:- materializing an object (Value -> JSON), where hidden fields are filtered out
- checking equality, again hidden fields are just ignored.
Also, keep in mind this is unrelated to private/public fields.
If this is not clear, please let me know!
@@ -221,7 +224,7 @@ arrayP = Fix <$> annotateLoc array | |||
objectP :: Parser Expr' | |||
objectP = Fix <$> annotateLoc object | |||
where | |||
object = mkObjectF <$> braces ((try methodP <|> pairP) `sepBy` comma) | |||
object = mkObjectF <$> braces ((try hidden <|> try methodP <|> pairP) `sepBy` comma) |
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.
Methods can be hidden too, but here it would fail to parse { foo :: function(x) x }
.
Here I would suggest changing the result to KeyValue key value hidden
, where hidden
is derived from paring either symbol :
or ::
.
src/Language/Jsonnet/Common.hs
Outdated
@@ -65,7 +65,7 @@ instance Alpha UnyOp | |||
data KeyValue a = KeyValue | |||
{ key :: a, | |||
value :: a | |||
} | |||
} | Hidden { key :: a } |
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.
Hidden fields, also have values, e.g.
{ foo : "bar", baz(x) :: x + 1 }
In this case, you might use the baz method in the program, regardless of the object being serialized to JSON.
As an example, the functions in the standard library are all hidden fields of the std object (https://jsonnet.org/ref/stdlib.html)
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.
My suggestion here would be to add an additional field hidden :: Bool
to the KeyValue
constructor.
Thanks for the suggestions @moleike I'll review them later today. |
Hi @moleike I tried your approach but I got stuck with this type error: ...l/jsonnet-haskell/src/Language/Jsonnet/Std.hs:42:26: error:
• No instance for (Integral Key) arising from a use of ‘inj’
• In the expression: inj (H.keys @Key @Value)
In the expression: ("objectFields", inj (H.keys @Key @Value))
In the expression:
[("assertEqual", inj assertEqual), ("type", inj valueType),
("isString", inj (isType "string")),
("isBoolean", inj (isType "boolean")), ....]
|
42 | ("objectFields", inj (H.keys @Key @Value)),
Please, let me know if I'm going in the right direction or I lost at some point. I understood that |
I managed to fix the error type, but I'm not sure about my implementation. |
Hi @CristhianMotoche, the problem you encounter is due to the impl. of diff --git a/src/Language/Jsonnet/Eval.hs b/src/Language/Jsonnet/Eval.hs
index 8dc0428..d0c0993 100644
--- a/src/Language/Jsonnet/Eval.hs
+++ b/src/Language/Jsonnet/Eval.hs
@@ -124,10 +124,6 @@ instance HasValue Double where
proj v = throwTypeMismatch "number" v
inj = VNum
-instance HasValue Key where
- proj = undefined
- inj = undefined
-
instance {-# OVERLAPS #-} Integral a => HasValue a where
proj (VNum n) = pure (round n)
proj v = throwTypeMismatch "number" v
diff --git a/src/Language/Jsonnet/Std.hs b/src/Language/Jsonnet/Std.hs
index ba2a1d3..2f8d67a 100644
--- a/src/Language/Jsonnet/Std.hs
+++ b/src/Language/Jsonnet/Std.hs
@@ -9,6 +9,7 @@ import Control.Applicative
import Control.Monad.Except
import qualified Data.ByteString as B
import Data.Foldable
+import Data.HashMap.Lazy (HashMap)
import qualified Data.HashMap.Lazy as H
import Data.List
import Data.Text (Text)
@@ -39,7 +40,7 @@ std = VObj $ (Thunk . pure) <$> H.fromList (map (\(k, v) -> (Visible k, v)) xs)
("isObject", inj (isType "object")),
("isArray", inj (isType "array")),
("isFunction", inj (isType "function")),
- ("objectFields", inj (H.keys @Key @Value)),
+ ("objectFields", inj objectFields),
("length", inj length'),
("abs", inj (abs @Double)),
("sign", inj (signum @Double)), -- incl. 0.0, (-0.0), and NaN
@@ -98,6 +99,10 @@ std = VObj $ (Thunk . pure) <$> H.fromList (map (\(k, v) -> (Visible k, v)) xs)
("reverse", inj (reverse @Value))
]
+
+objectFields :: HashMap Key Value -> [Text]
+objectFields o = [k | Visible k <- H.keys o]
+
assertEqual :: Value -> Value -> Eval Bool
assertEqual a b = do
a' <- manifest a |
That's correct.
Yes you are 👍 |
Thank you so much for you kind guidance @moleike Let me know if you have any other comment/suggestion. 😃 |
@CristhianMotoche I think the issue is solved with your changes. Congrats on tackling this issue! |
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.
LGTM
There's one minor issue remaining, but it can be fixed later on: |
Solves #2
Changes
Hidded
, which is a new constructor forKeyValue
(VNull, undefined)
undefined
won't be evaluated because its filtered from the KeyValue listPlease, let me know if this would solve the issue. 😃