Unverified Commit 4fbf6bf1 authored by Silvan Mosberger's avatar Silvan Mosberger Committed by GitHub
Browse files

pkgs-lib.formats.xml: init (#342633)

parents 77156bcc a468405d
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -343,6 +343,21 @@ have a predefined type and string generator already declared under
    and returning a set with TOML-specific attributes `type` and
    `generate` as specified [below](#pkgs-formats-result).

`pkgs.formats.xml` { format ? "badgerfish", withHeader ? true}

:   A function taking an attribute set with values
    and returning a set with XML-specific attributes `type` and
    `generate` as specified [below](#pkgs-formats-result).

    `format`

    :   Input format. Because XML can not be translated one-to-one, we have to use intermediate formats. Possible values:
      - `"badgerfish"`: Uses [badgerfish](http://www.sklar.com/badgerfish/) conversion.

    `withHeader`

    :   Outputs the xml with header.

`pkgs.formats.cdn` { }

:   A function taking an empty attribute set (for future extensibility)
+60 −0
Original line number Diff line number Diff line
@@ -577,4 +577,64 @@ rec {
    '') {};
  };

  xml =
    {
      format ? "badgerfish",
      withHeader ? true,
    }:
    if format == "badgerfish" then
      {
        type = let
          valueType = nullOr (oneOf [
            bool
            int
            float
            str
            path
            (attrsOf valueType)
            (listOf valueType)
          ]) // {
            description = "XML value";
          };
        in valueType;

        generate =
          name: value:
          pkgs.callPackage (
            {
              runCommand,
              python3,
              libxml2Python,
            }:
            runCommand name
              {
                nativeBuildInputs = [
                  python3
                  python3.pkgs.xmltodict
                  libxml2Python
                ];
                value = builtins.toJSON value;
                pythonGen = ''
                  import json
                  import os
                  import xmltodict

                  with open(os.environ["valuePath"], "r") as f:
                      print(xmltodict.unparse(json.load(f), full_document=${toString withHeader}, pretty=True, indent=" " * 2))
                '';
                passAsFile = [
                  "value"
                  "pythonGen"
                ];
                preferLocalBuild = true;
              }
              ''
                python3 "$pythonGenPath" > $out
                xmllint $out > /dev/null
              ''
          ) { };
      }
    else
      throw "pkgs.formats.xml: Unknown format: ${format}";

}
+27 −0
Original line number Diff line number Diff line
@@ -644,4 +644,31 @@ in runBuildTests {
    '';
  };

  badgerfishToXmlGenerate = shouldPass {
    format = formats.xml { };
    input = {
      root = {
        "@id" = "123";
        "@class" = "example";
        child1 = {
          "@name" = "child1Name";
          "#text" = "text node";
        };
        child2 = {
          grandchild = "This is a grandchild text node.";
        };
        nulltest = null;
      };
    };
    expected = ''
      <?xml version="1.0" encoding="utf-8"?>
      <root class="example" id="123">
        <child1 name="child1Name">text node</child1>
        <child2>
          <grandchild>This is a grandchild text node.</grandchild>
        </child2>
        <nulltest></nulltest>
      </root>
    '';
  };
}