Commit c5cdad7d authored by Felix Buehler's avatar Felix Buehler
Browse files

pkgs-lib.formats: add mkStructuredType

parent 5e1f5b62
Loading
Loading
Loading
Loading
+41 −120
Original line number Diff line number Diff line
@@ -126,14 +126,23 @@ optionalAttrs allowAliases aliases

  php = (import ./formats/php/default.nix { inherit lib pkgs; }).format;

  json =
    { }:
    {
  /*
    Creates a structured value type suitable for serialization formats.

      type =
    Parameters:
    - typeName: String describing the format (e.g. "JSON", "YAML", "XML")

    Returns a type suitable for structured data formats that supports:
    - Basic types: boolean, integer, float, string, path
    - Complex types: attribute sets and lists
  */
  mkStructuredType =
    {
      typeName,
      nullable ? true,
    }:
    let
          valueType =
            nullOr (oneOf [
      baseType = oneOf [
        bool
        int
        float
@@ -141,13 +150,19 @@ optionalAttrs allowAliases aliases
        path
        (attrsOf valueType)
        (listOf valueType)
            ])
            // {
              description = "JSON value";
      ];
      valueType = (if nullable then nullOr baseType else baseType) // {
        description = "${typeName} value";
      };
    in
    valueType;

  json =
    { }:
    {

      type = mkStructuredType { typeName = "JSON"; };

      generate =
        name: value:
        pkgs.callPackage (
@@ -187,23 +202,7 @@ optionalAttrs allowAliases aliases
            ''
        ) { };

      type =
        let
          valueType =
            nullOr (oneOf [
              bool
              int
              float
              str
              path
              (attrsOf valueType)
              (listOf valueType)
            ])
            // {
              description = "YAML 1.1 value";
            };
        in
        valueType;
      type = mkStructuredType { typeName = "YAML 1.1"; };

    };

@@ -226,23 +225,7 @@ optionalAttrs allowAliases aliases
            ''
        ) { };

      type =
        let
          valueType =
            nullOr (oneOf [
              bool
              int
              float
              str
              path
              (attrsOf valueType)
              (listOf valueType)
            ])
            // {
              description = "YAML 1.2 value";
            };
        in
        valueType;
      type = mkStructuredType { typeName = "YAML 1.2"; };

    };

@@ -491,23 +474,7 @@ optionalAttrs allowAliases aliases
    { }:
    json { }
    // {
      type =
        let
          valueType =
            oneOf [
              bool
              int
              float
              str
              path
              (attrsOf valueType)
              (listOf valueType)
            ]
            // {
              description = "TOML value";
            };
        in
        valueType;
      type = mkStructuredType { typeName = "TOML"; };

      generate =
        name: value:
@@ -540,23 +507,7 @@ optionalAttrs allowAliases aliases
    { }:
    json { }
    // {
      type =
        let
          valueType =
            nullOr (oneOf [
              bool
              int
              float
              str
              path
              (attrsOf valueType)
              (listOf valueType)
            ])
            // {
              description = "CDN value";
            };
        in
        valueType;
      type = mkStructuredType { typeName = "CDN"; };

      generate =
        name: value:
@@ -971,23 +922,9 @@ optionalAttrs allowAliases aliases
  pythonVars =
    { }:
    {
      type =
        let
          valueType =
            nullOr (oneOf [
              bool
              float
              int
              path
              str
              (attrsOf valueType)
              (listOf valueType)
            ])
            // {
              description = "Python value";
            };
        in
        attrsOf valueType;
      type = attrsOf (mkStructuredType {
        typeName = "Python";
      });

      lib = {
        mkRaw = value: {
@@ -1067,23 +1004,7 @@ optionalAttrs allowAliases aliases
    }:
    if format == "badgerfish" then
      {
        type =
          let
            valueType =
              nullOr (oneOf [
                bool
                int
                float
                str
                path
                (attrsOf valueType)
                (listOf valueType)
              ])
              // {
                description = "XML value";
              };
          in
          valueType;
        type = mkStructuredType { typeName = "XML"; };

        generate =
          name: value: