Skip to content

Commit

Permalink
dhall-to-nix: Only double-quote symbols if they have symbols
Browse files Browse the repository at this point in the history
A small improvement in the generation logic.
The hnix should really just do this for us.
  • Loading branch information
Profpatsch committed Feb 15, 2024
1 parent 34a797e commit 7a7e767
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions dhall-nix/src/Dhall/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ dhallToNix e =
return Nix.mkNull
loop (App (Field (Union _kts) (Dhall.Core.fieldSelectionLabel -> k)) v) = do
v' <- loop v
return (unionChoice k (Just v'))
return (unionChoice (VarName k) (Just v'))
loop (App a b) = do
a' <- loop a
b' <- loop b
Expand Down Expand Up @@ -604,7 +604,7 @@ dhallToNix e =
-- see https://github.com/dhall-lang/dhall-haskell/issues/2414
nixAttrs pairs =
Fix $ NSet NonRecursive $
(\(key, val) -> NamedVar ((mkDoubleQuoted key) :| []) val Nix.nullPos)
(\(key, val) -> NamedVar ((mkDoubleQuotedIfNecessary (VarName key)) :| []) val Nix.nullPos)
<$> pairs
loop (Union _) = return untranslatable
loop (Combine _ _ a b) = do
Expand Down Expand Up @@ -693,11 +693,11 @@ dhallToNix e =
-- (here "x").
--
-- This translates `< Foo : T >.Foo` to `x: { Foo }: Foo x`
Just (Just _) -> return ("x" ==> (unionChoice k (Just "x")))
_ -> return (unionChoice k Nothing)
Just (Just _) -> return ("x" ==> (unionChoice (VarName k) (Just "x")))
_ -> return (unionChoice (VarName k) Nothing)
loop (Field a (Dhall.Core.fieldSelectionLabel -> b)) = do
a' <- loop a
return (Fix (Nix.NSelect a' (mkDoubleQuoted b :| []) Nothing))
return (Fix (Nix.NSelect Nothing a' (mkDoubleQuotedIfNecessary (VarName b) :| [])))
loop (Project a (Left b)) = do
a' <- loop a
return (Nix.mkNonRecSet [ Nix.inheritFrom a' (fmap VarName b) ])
Expand Down Expand Up @@ -734,9 +734,9 @@ dhallToNix e =
-- so we generate @union: union."Frob/Baz"@ instead.
--
-- If passArgument is @Just@, pass the argument to the union selector.
unionChoice :: Text -> Maybe NExpr -> NExpr
unionChoice :: VarName -> Maybe NExpr -> NExpr
unionChoice chosenKey passArgument =
let selector = Fix (Nix.NSelect Nothing (Nix.mkSym "u") (mkDoubleQuoted chosenKey :| []))
let selector = Fix (Nix.NSelect Nothing (Nix.mkSym "u") (mkDoubleQuotedIfNecessary chosenKey :| []))
in Nix.Param "u" ==>
case passArgument of
Nothing -> selector
Expand All @@ -750,8 +750,15 @@ unionChoice chosenKey passArgument =
-- where
--
-- @{ foo/bar = 42; }.foo/bar@ is not syntactically valid nix.
mkDoubleQuoted :: Text -> NKeyName r
mkDoubleQuoted key = DynamicKey (Plain (DoubleQuoted [Plain key]))
--
-- This is only done if necessary (where “necessary” is not super defined right now).
mkDoubleQuotedIfNecessary :: VarName -> NKeyName r
mkDoubleQuotedIfNecessary key@(VarName keyName) =
if Text.all simpleChar keyName
then StaticKey key
else DynamicKey (Plain (DoubleQuoted [Plain keyName]))
where
simpleChar c = isAsciiLower c || isAsciiUpper c


-- | Nix does not support symbols like @foo/bar@, but they are allowed in dhall.
Expand Down

0 comments on commit 7a7e767

Please sign in to comment.