From 9aab03b19af98aad992c9e67ed56ae9017db0b87 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sun, 25 Dec 2022 19:33:34 +0100 Subject: [PATCH] dhall-to-nix: Only double-quote symbols if they have symbols A small improvement in the generation logic. The hnix should really just do this for us. --- dhall-nix/src/Dhall/Nix.hs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/dhall-nix/src/Dhall/Nix.hs b/dhall-nix/src/Dhall/Nix.hs index 7d22f50b3..b858ead44 100644 --- a/dhall-nix/src/Dhall/Nix.hs +++ b/dhall-nix/src/Dhall/Nix.hs @@ -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 @@ -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) ]) @@ -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 @@ -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.