Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkgs-lib.formats: add mkStructuredType #372501

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 25 additions & 73 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 Expand Up @@ -584,19 +548,7 @@ rec {
}:
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:
Expand Down