-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add built in version coercion #43
Open
jamesrswift
wants to merge
5
commits into
main
Choose a base branch
from
11-add-built-in-version-schema-and-coercion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,5 @@ | ||
|
||
/// If the tested value is not already of dictionary type, the function provided as argument is expected to return a dictionary type with a shape that passes validation. | ||
/// | ||
/// #example[``` | ||
/// #let schema = z.dictionary( | ||
/// pre-transform: z.coerce.dictionary((it)=>(name: it)), | ||
/// (name: z.string()) | ||
/// ) | ||
/// | ||
/// #z.parse("Hello", schema) \ | ||
/// #z.parse((name: "Hello"), schema) | ||
/// ```] | ||
/// | ||
/// - fn (function): Transformation function that the tested value and returns a dictionary that has a shape that passes validation. | ||
#let dictionary(fn) = (self, it) => { | ||
if (type(it) != type((:))) { | ||
return fn(it) | ||
} | ||
it | ||
} | ||
|
||
/// If the tested value is not already of array type, it is transformed into an array of size 1 | ||
/// | ||
/// #example[``` | ||
/// #let schema = z.array( | ||
/// pre-transform: z.coerce.array, | ||
/// z.string() | ||
/// ) | ||
/// | ||
/// #z.parse("Hello", schema) \ | ||
/// #z.parse(("Hello", "world"), schema) | ||
/// ```] | ||
#let array(self, it) = { | ||
if (type(it) != type(())) { | ||
return (it,) | ||
} | ||
it | ||
} | ||
|
||
/// Tested value is forceably converted to content type | ||
/// | ||
/// #example[``` | ||
/// #let schema = z.content( | ||
/// pre-transform: z.coerce.content | ||
/// ) | ||
/// | ||
/// #type(z.parse("Hello", schema)) \ | ||
/// #type(z.parse(123456, schema)) | ||
/// ```] | ||
#let content(self, it) = [#it] | ||
|
||
/// An attempt is made to convert string, numeric, or dictionary inputs into datetime objects | ||
/// | ||
/// #example[``` | ||
/// #let schema = z.date( | ||
/// pre-transform: z.coerce.date | ||
/// ) | ||
/// | ||
/// #z.parse(2020, schema) \ | ||
/// #z.parse("2020-03-15", schema) \ | ||
/// #z.parse("2020/03/15", schema) \ | ||
/// #z.parse((year: 2020, month: 3, day: 15), schema) \ | ||
/// ```] | ||
#let date(self, it) = { | ||
if (type(it) == type(datetime.today())) { | ||
return it | ||
} | ||
if (type(it) == int) { | ||
// assume this is the year | ||
assert( | ||
it > 1000 and it < 3000, | ||
message: "The date is assumed to be a year between 1000 and 3000", | ||
) | ||
return datetime(year: it, month: 1, day: 1) | ||
} | ||
|
||
if (type(it) == str) { | ||
let yearMatch = it.find(regex(`^([1|2])([0-9]{3})$`.text)) | ||
if (yearMatch != none) { | ||
// This isn't awesome, but probably fine | ||
return datetime(year: int(it), month: 1, day: 1) | ||
} | ||
let dateMatch = it.find( | ||
regex(`^([1|2])([0-9]{3})([-\/])([0-9]{1,2})([-\/])([0-9]{1,2})$`.text), | ||
) | ||
if (dateMatch != none) { | ||
let parts = it.split(regex("[-\/]")) | ||
return datetime( | ||
year: int(parts.at(0)), | ||
month: int(parts.at(1)), | ||
day: int(parts.at(2)), | ||
) | ||
} | ||
panic("Unknown datetime object from string, try: `2020/03/15` as YYYY/MM/DD, also accepts `2020-03-15`") | ||
} | ||
|
||
if (type(it) == type((:))) { | ||
if ("year" in it) { | ||
return return datetime( | ||
year: it.at("year"), | ||
month: it.at("month", default: 1), | ||
day: it.at("day", default: 1), | ||
) | ||
} | ||
panic("Unknown datetime object from dictionary, try: `(year: 2022, month: 2, day: 3)`") | ||
} | ||
panic("Unknown date of type '" + type(it) + "' accepts: datetime, str, int, and object") | ||
|
||
} | ||
#import "coercions/dictionary.typ": dictionary | ||
#import "coercions/array.typ": array | ||
#import "coercions/content.typ": content | ||
#import "coercions/date.typ": date | ||
#import "coercions/version.typ": version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// If the tested value is not already of array type, it is transformed into an array of size 1 | ||
/// | ||
/// #example[``` | ||
/// #let schema = z.array( | ||
/// pre-transform: z.coerce.array, | ||
/// z.string() | ||
/// ) | ||
/// | ||
/// #z.parse("Hello", schema) \ | ||
/// #z.parse(("Hello", "world"), schema) | ||
/// ```] | ||
#let array(self, it) = { | ||
if (type(it) != type(())) { | ||
return (it,) | ||
} | ||
it | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
/// Tested value is forceably converted to content type | ||
/// | ||
/// #example[``` | ||
/// #let schema = z.content( | ||
/// pre-transform: z.coerce.content | ||
/// ) | ||
/// | ||
/// #type(z.parse("Hello", schema)) \ | ||
/// #type(z.parse(123456, schema)) | ||
/// ```] | ||
#let content(self, it) = [#it] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/// An attempt is made to convert string, numeric, or dictionary inputs into datetime objects | ||
/// | ||
/// #example[``` | ||
/// #let schema = z.date( | ||
/// pre-transform: z.coerce.date | ||
/// ) | ||
/// | ||
/// #z.parse(2020, schema) \ | ||
/// #z.parse("2020-03-15", schema) \ | ||
/// #z.parse("2020/03/15", schema) \ | ||
/// #z.parse((year: 2020, month: 3, day: 15), schema) \ | ||
/// ```] | ||
#let date(self, it) = { | ||
if (type(it) == type(datetime.today())) { | ||
return it | ||
} | ||
if (type(it) == int) { | ||
// assume this is the year | ||
assert( | ||
it > 1000 and it < 3000, | ||
message: "The date is assumed to be a year between 1000 and 3000", | ||
) | ||
return datetime(year: it, month: 1, day: 1) | ||
} | ||
|
||
if (type(it) == str) { | ||
let yearMatch = it.find(regex(`^([1|2])([0-9]{3})$`.text)) | ||
if (yearMatch != none) { | ||
// This isn't awesome, but probably fine | ||
return datetime(year: int(it), month: 1, day: 1) | ||
} | ||
let dateMatch = it.find( | ||
regex(`^([1|2])([0-9]{3})([-\/])([0-9]{1,2})([-\/])([0-9]{1,2})$`.text), | ||
) | ||
if (dateMatch != none) { | ||
let parts = it.split(regex("[-\/]")) | ||
return datetime( | ||
year: int(parts.at(0)), | ||
month: int(parts.at(1)), | ||
day: int(parts.at(2)), | ||
) | ||
} | ||
panic("Unknown datetime object from string, try: `2020/03/15` as YYYY/MM/DD, also accepts `2020-03-15`") | ||
} | ||
|
||
if (type(it) == type((:))) { | ||
if ("year" in it) { | ||
return return datetime( | ||
year: it.at("year"), | ||
month: it.at("month", default: 1), | ||
day: it.at("day", default: 1), | ||
) | ||
} | ||
panic("Unknown datetime object from dictionary, try: `(year: 2022, month: 2, day: 3)`") | ||
} | ||
panic("Unknown date of type '" + type(it) + "' accepts: datetime, str, int, and object") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
/// If the tested value is not already of dictionary type, the function provided as argument is expected to return a dictionary type with a shape that passes validation. | ||
/// | ||
/// #example[``` | ||
/// #let schema = z.dictionary( | ||
/// pre-transform: z.coerce.dictionary((it)=>(name: it)), | ||
/// (name: z.string()) | ||
/// ) | ||
/// | ||
/// #z.parse("Hello", schema) \ | ||
/// #z.parse((name: "Hello"), schema) | ||
/// ```] | ||
/// | ||
/// - fn (function): Transformation function that the tested value and returns a dictionary that has a shape that passes validation. | ||
#let dictionary(fn) = (self, it) => { | ||
if (type(it) != type((:))) { | ||
return fn(it) | ||
} | ||
it | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#let stdversion = version | ||
|
||
/// Tested value is forceably converted to version type | ||
/// | ||
/// #example[``` | ||
/// #let schema = z.version( | ||
/// pre-transform: z.coerce.version | ||
/// ) | ||
/// | ||
/// #type(z.parse("0.1.1", schema)) \ | ||
/// #type(z.parse(1, schema)) | ||
/// #type(z.parse((1,1,), schema)) | ||
/// ```] | ||
#let version(self, it) = { | ||
if type(it) == str { | ||
return stdversion( | ||
it.split(".") | ||
.filter(it=>it!="") | ||
.map(int) | ||
) | ||
} else if type(it) == int { | ||
return stdversion(it) | ||
} else if type(it) == array { | ||
return stdversion(it.map(int)) | ||
} | ||
it | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#import "/src/lib.typ" as z | ||
#set page(height: auto, width: auto) | ||
|
||
#let test(x) = z.parse(x, z.version(pre-transform: z.coerce.version)) | ||
|
||
#repr(test(1)) | ||
|
||
#repr(test((1,1))) | ||
|
||
#repr(test("1.1.")) | ||
|
||
#repr(test("1.0.1.0")) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really unsure about this one, seems more surprising than useful to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just a file being moved, definitely worth being looked at but I'm not sure if its for this PR (which deals with just the version coercion).
On the topic of datetime parsing, I think dictionaries should be supported, on ISO formats, ISO 8601? I wouldn't support offsets but other than that, that should be doable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, dictionaries sound fine to me.
Yeah, the full year, full month and full day must be specified for a string to be a valid date, i.e.
"07"
or"30-5"
are rejected, because they are ambiguous,"1970-01-01"
or"1970-1-1"
are valid. No further checks like checking for the max day of month, that's up to the impl ofdatetime
and more complicated than necessary.