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

improve introspection of types #38

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/base-type.typ
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/// Schema generator. Provides default values for when defining custom types.
#let base-type(
name: "unknown",
description: none,
optional: false,
default: none,
types: (),
Expand All @@ -21,6 +22,7 @@
return (
valkyrie-type: true,
name: name,
description: if (description != none){ description } else { name },
optional: optional,
default: default,
types: types,
Expand Down
2 changes: 1 addition & 1 deletion src/schemas/enumerations.typ
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#import "../types.typ" as z;

#let papersize = z.choice.with(
name: "paper size",
description: "paper size",
(
"a0",
"a1",
Expand Down
3 changes: 2 additions & 1 deletion src/types/array.typ
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
assert-positive-type(max, types: (int,), name: "Maximum length")

base-type(
name: "array[" + (descendents-schema.name) + "]",
name: name,
description: name + "[" + (descendents-schema.description) + "]",
default: (),
types: (array-type,),
..args.named(),
Expand Down
11 changes: 4 additions & 7 deletions src/types/dictionary.typ
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@
/// -> schema
#let dictionary(
dictionary-schema,
name: "dictionary",
default: (:),
optional: false,
assertions: (),
pre-transform: (self, it) => it,
post-transform: (self, it) => it,
aliases: (:),
..args,
) = {

assert-base-type-dictionary(dictionary-schema)

base-type(
name: "dictionary",
optional: optional,
name: name,
default: default,
types: (dictionary-type,),
assertions: assertions,
pre-transform: (self, it) => {
it = pre-transform(self, it)
for (src, dst) in aliases {
Expand All @@ -37,7 +34,7 @@
}
return it
},
post-transform: post-transform,
..args.named()
) + (
dictionary-schema: dictionary-schema,
handle-descendents: (self, it, ctx: z-ctx(), scope: ()) => {
Expand Down
5 changes: 3 additions & 2 deletions src/types/logical.typ
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
assert-base-type-array(args.pos(), scope: ("arguments",))

base-type(
name: "[" + args.pos().map(it => it.name).join(", ", last: " or ") + "]",
name: "either",
description: "[" + args.pos().map(it => it.name).join(", ", last: " or ") + "]",
..args.named(),
) + (
strict: strict,
Expand All @@ -36,7 +37,7 @@
}

let message = (
"Type failed to match any of possible options: " + self.options.map(it => it.name).join(
"Type failed to match any of possible options: " + self.options.map(it => it.description).join(
", ",
last: " or ",
) + ". Got " + type(it)
Expand Down
4 changes: 2 additions & 2 deletions src/types/number.typ
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
)
}

#let integer = number.with(name: "integer", types: (int,))
#let floating-point = number.with(name: "float", types: (float,))
#let integer = number.with(description: "integer", types: (int,))
#let floating-point = number.with(description: "float", types: (float,))
6 changes: 2 additions & 4 deletions src/types/string.typ
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
/// Valkyrie schema generator for strings
///
/// -> schema
#let string = base-type.with(name: "string", types: (str,))

#let string(
assertions: (),
min: none,
Expand Down Expand Up @@ -39,7 +37,7 @@
}

#let email = string.with(
name: "email",
description: "email",
assertions: (
matches(
regex("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]{2,3}){1,2}$"),
Expand All @@ -49,7 +47,7 @@
);

#let ip = string.with(
name: "ip",
description: "ip",
assertions: (
matches(
regex("^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$"),
Expand Down
Loading