[Nix-dev] [PATCH] t/allow-arbitrary-strinsg-in-names

Marc Weber marco-oweber at gmx.de
Tue Jul 13 00:58:13 CEST 2010


allow strings as names in attribute sets:

Example:

let sourcesByVersion = {
  "git-1.8" : fetchurl ..
  "git-2.0" : fetchurl ..
}

This isn't something new. This undocumented feature was available for a long
time using listToAttrs which accepted arbitrary strings as name.

Signed-off-by: Marc Weber <marco-oweber at gmx.de>
---
 doc/manual/writing-nix-expressions.xml          |    1 +
 src/libexpr/parser.y                            |   11 +++++++++++
 tests/lang/eval-okay-attrs-strings-as-names.exp |    1 +
 tests/lang/eval-okay-attrs-strings-as-names.nix |    4 ++++
 4 files changed, 17 insertions(+), 0 deletions(-)
 create mode 100644 tests/lang/eval-okay-attrs-strings-as-names.exp
 create mode 100644 tests/lang/eval-okay-attrs-strings-as-names.nix

diff --git a/doc/manual/writing-nix-expressions.xml b/doc/manual/writing-nix-expressions.xml
index 35fdf53..91c254d 100644
--- a/doc/manual/writing-nix-expressions.xml
+++ b/doc/manual/writing-nix-expressions.xml
@@ -800,6 +800,7 @@ by a semicolon.  For example:
 { x = 123;
   text = "Hello";
   y = f { bla = 456; };
+  "spaces - allowed 2.8" = "value";
 }</programlisting>
 
 This defines an attribute set with attributes named
diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y
index 7236bab..f207567 100644
--- a/src/libexpr/parser.y
+++ b/src/libexpr/parser.y
@@ -402,6 +402,17 @@ ids
 
 attrpath
   : attrpath '.' ID { $$ = $1; $1->push_back(data->symbols.create($3)); }
+  | '"' STR '"' {
+    $$ = new vector<Symbol>;
+    Expr * x = $2;
+    if(ExprString* str = dynamic_cast<ExprString*>(x))
+      $$->push_back(data->symbols.create(str->s));
+    else
+      // should never be reached - you never know.
+      // should static_cast be used instead? Or should I add a new type passed
+      // from lexer?
+      throw ParseError(format("ExprString expected"));
+  }
   | ID { $$ = new vector<Symbol>; $$->push_back(data->symbols.create($1)); }
   ;
 
diff --git a/tests/lang/eval-okay-attrs-strings-as-names.exp b/tests/lang/eval-okay-attrs-strings-as-names.exp
new file mode 100644
index 0000000..d9c5065
--- /dev/null
+++ b/tests/lang/eval-okay-attrs-strings-as-names.exp
@@ -0,0 +1 @@
+"testcaseok"
diff --git a/tests/lang/eval-okay-attrs-strings-as-names.nix b/tests/lang/eval-okay-attrs-strings-as-names.nix
new file mode 100644
index 0000000..42d50cc
--- /dev/null
+++ b/tests/lang/eval-okay-attrs-strings-as-names.nix
@@ -0,0 +1,4 @@
+let attr = {
+  "key 1" = "test";
+  "key 2" = "caseok";
+}; in "${builtins.getAttr "key 1" attr}${builtins.getAttr "key 2" attr}"
-- 
1.7.1




More information about the nix-dev mailing list