Feat: construct datetime from record#15455
Conversation
f069368 to
95fe428
Compare
|
The existing {a: (date now | into int), b: blub} | into datetime aI think we never added it because there will be a type inference challenge Given a record input type it will be undecidable what the output type will be. |
|
@sholderbach I would be in favor of removing this field-mapping behavior since it's simple to use |
|
@LoicRiegel First of all, thanks for implementing this! In previous discussions about this conversion on discord we ended up with some interesting semantics regarding missing fields.
Can you implement this behavior? You can use my half-baked implementation as reference. |
Is it functional, thoug? > {a: (date now | into int), b: blub} | into datetime a
╭───┬──────╮
│ a │ now │
│ b │ blub │
╰───┴──────╯
> {a: (date now | into int), b: blub} | into datetime a | describe
record<a: date, b: string>
I would also be in favor of removing that, especially if it has never worked, but also this would make the signature clearer. |
Thank you :)
|
|
I'm thinking, if users want to use his/her current location and datetime, something like this would work > date now | into record | update hour 5 | into datetime
Tue, 1 Apr 2025 05:54:54 +0200 (9 hours ago) |
> "5:00" | into datetime | to nuon
2025-04-01T05:00:00+03:00It makes sense to me to have the same behavior for records. > {hour: 5} | into datetime | to nuon
2025-04-01T05:00:00+03:00As for deterministic behavior, because the fields smaller than the largest specified field default to 0, specifying the year and the timezone in the input makes the output deterministic. > {year: 2025, month: 12, day: 6, hour: 16, minute: 0, second: 59, timezone: "+00:00"} | into datetime | to nuon
2025-12-06T16:00:59+00:00
I don't think specifying the timezone should affect the whether the current time or 0 is used. If such an option is necessary, I would rather have it as a separate flag.
Agreed. Adding a few more examples that demonstrate this behavior would be nice too. |
Oh I didn't realize that! In that case, I totally agree with you, we should definitely have the same behavior.
Okay fair enough
Yeah absolutely; forget everything I said about the possible strategies
Ok for a couple of examples, and maybe I'll check the documentation website. |
Your example show that it is in fact functional in that it converts the scalar values in the given columns. I put the topic of this behavior on the meetings agenda for tomorrow. We have a ton of commands that accept optional columns (and thus operate on whole tables or records on the given columns) The alternative for multiple columns is pretty wordy Either multiple |
You're right, I don't know what I what I was thinking. I guess I was visually expecting the full string describing the date, and not just "now" in the table. |
|
I am gonna mark this as draft until we have a recommendation from the meeting. Otherwise merging this could lead to typecheck bugs in the following situation produces-record | into datetime $col | expects-record
produces-record | into datetime | expects-datetime |
|
Update after I worked on it tonight Done:
BTW the code grew quite a bit, I'm happy to take any improvement suggestions:) Left to do:
|
|
Okay, I just pushed the last commits, so now we take the local timezone if the record does not contain it. I updated the examples in the PR description The PR is ready for review again :) |
89fc02a to
40f2840
Compare
|
I just rebased to resolve the conflicts |
sholderbach
left a comment
There was a problem hiding this comment.
Thanks for the thorough testing including the error paths and your extensive description of those in the PR note.
| } | ||
| } | ||
|
|
||
| fn merge_record(record: &Record, head: Span, span: Span) -> Value { |
There was a problem hiding this comment.
Minor thought, if this were -> Result<Value, ShellError> the internal code could simplfy quite a bit with ? and .map_err, at the cost of having to unify that for the fn action() signature later.
But that is not critical for the funciton.
There was a problem hiding this comment.
Okay, thanks for the tip!
I wanted to implement the construct from record for into duration too, so maybe I'll have a look then.
You're welcome! It's worth it 😄 |
|
I'll also write something down in the release notes about this one |
Also could you please close the issue? |
Issue #12289, can be closed when this is merged
Description
Currently, the
into datetimecommand's signature indicates that it supports input as record, but it was actually not supported.This PR implements this feature.
User-Facing Changes
into datetime's signature changed (see comments)Happy paths
Note: I'm in +02:00 timezone.
Edge cases
{} | into datetime Fri, 4 Apr 2025 18:35:19 +0200 (now)Error paths
Tests + Formatting
Tests added
Fmt + clippy OK
After Submitting
Maybe indicate that in the release notes
I added an example in the command, so the documentation will be automatically updated.