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 Dec 25, 2022
1 parent 2e26784 commit 9aab03b
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions dhall-nix/src/Dhall/Nix.hs
Original file line number Diff line number Diff line change
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 key) :| []) val Nix.nullPos)
<$> pairs
loop (Union _) = return untranslatable
loop (Combine _ _ a b) = do
Expand Down Expand Up @@ -697,7 +697,7 @@ dhallToNix e =
_ -> return (unionChoice k Nothing)
loop (Field a (Dhall.Core.fieldSelectionLabel -> b)) = do
a' <- loop a
return (Fix (Nix.NSelect Nothing a' (mkDoubleQuoted b :| [])))
return (Fix (Nix.NSelect Nothing a' (mkDoubleQuotedIfNecessary b :| [])))
loop (Project a (Left b)) = do
a' <- loop a
return (Nix.mkNonRecSet [ Nix.inheritFrom a' (fmap VarName b) ])
Expand Down Expand Up @@ -736,7 +736,7 @@ dhallToNix e =
-- If passArgument is @Just@, pass the argument to the union selector.
unionChoice :: Text -> 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 :: Text -> NKeyName r
mkDoubleQuotedIfNecessary key =
if Text.all simpleChar key
then StaticKey (VarName key)
else DynamicKey (Plain (DoubleQuoted [Plain key]))
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 9aab03b

Please sign in to comment.