Skip to content

Commit

Permalink
pkgs-lib.formats: add mkStructuredType
Browse files Browse the repository at this point in the history
  • Loading branch information
Stunkymonkey committed Jan 13, 2025
1 parent 0b2095f commit 7f6a0a2
Showing 1 changed file with 24 additions and 60 deletions.
84 changes: 24 additions & 60 deletions pkgs/pkgs-lib/formats.nix
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,51 @@ rec {
inherit (lib.types) nullOr oneOf coercedTo listOf nonEmptyListOf attrsOf either;
inherit (lib.types) bool int float str path;

json = {}: {
/* Creates a structured value type suitable for serialization formats.
type = let
valueType = nullOr (oneOf [
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 }: let
baseType = oneOf [
bool
int
float
str
path
(attrsOf valueType)
(listOf valueType)
]) // {
description = "JSON value";
];
valueType = (nullOr baseType) // {
description = "${typeName} value";
};
in valueType;

json = {}: {

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

generate = name: value: pkgs.callPackage ({ runCommand, jq }: runCommand name {
nativeBuildInputs = [ jq ];
value = builtins.toJSON value;
passAsFile = [ "value" ];
preferLocalBuild = true;
} ''
jq . "$valuePath"> $out
jq . "$valuePath" > $out
'') {};

};

yaml = yaml_1_1;

yaml_1_1 = {}: {

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

generate = name: value: pkgs.callPackage ({ runCommand, remarshal_0_17 }: runCommand name {
nativeBuildInputs = [ remarshal_0_17 ];
value = builtins.toJSON value;
Expand All @@ -87,21 +102,6 @@ rec {
} ''
json2yaml "$valuePath" "$out"
'') {};

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

};

# the ini formats share a lot of code
Expand Down Expand Up @@ -285,19 +285,7 @@ rec {
};

toml = {}: 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: pkgs.callPackage ({ runCommand, remarshal }: runCommand name {
nativeBuildInputs = [ remarshal ];
Expand All @@ -319,19 +307,7 @@ rec {
Currently used by Panda, Reposilite, and FunnyGuilds (as per the repo's readme).
*/
cdn = {}: 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: pkgs.callPackage ({ runCommand, json2cdn }: runCommand name {
nativeBuildInputs = [ json2cdn ];
Expand Down Expand Up @@ -544,19 +520,7 @@ rec {
# Outputs a succession of Python variable assignments
# Useful for many Django-based services
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"; });
generate = name: value: pkgs.callPackage ({ runCommand, python3, black }: runCommand name {
nativeBuildInputs = [ python3 black ];
value = builtins.toJSON value;
Expand Down

0 comments on commit 7f6a0a2

Please sign in to comment.