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

feat: add from_unix_timestamp function #277

Merged
merged 5 commits into from
Jun 27, 2023
Merged

Conversation

pront
Copy link
Collaborator

@pront pront commented Jun 23, 2023

closes: #188

Contains unit tests. Also, tested with REPL:

.t = from_unix_timestamp!(5)
t'1970-01-01T00:00:05Z'

.t = from_unix_timestamp!(5, unit: "nanoseconds")
t'1970-01-01T00:00:00.000000005Z'

.t = from_unix_timestamp!(5.1)
error[E110]: invalid argument type
  ┌─ :1:27

This introduces some code duplication with stdlib/to_timestamp.rs but the latter will be deprecated soon.

@pront pront marked this pull request as draft June 23, 2023 21:58
@jszwedko jszwedko requested a review from fuchsnj June 23, 2023 22:05
src/stdlib/from_unix_timestamp.rs Show resolved Hide resolved
}

fn type_def(&self, state: &state::TypeState) -> TypeDef {
self.value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not quite right.

  • .fallible_unless(Kind::timestamp()) makes the type definition fallible unless the input kind is a timestamp. There are a few issues with this. First, the VRL compiler automatically checks function argument types, so you don't need to worry about checking those here. Second, timestamp is an invalid type here anyway.
  • with_kind completely overwrites the original kind (self.value) here, so there's no need to use self.value at all

There's 2 parts to a type definition, so lets look at the individually:

  • kind: This is the actual type returned by the function. This function will always return a timestamp, so it can just be hard-coded to Kind::timestamp().
  • fallibility: This is a boolean, saying if the function can fail (at runtime) or not. There's an interesting edge case here that I'll talk about below, but in general this function can always potentially fail. If you want to just hard-code this to fallible I'd be okay with that, so the entire type def becomes TypeDef::timestamp().fallible()

If we can prove at (VRL) compile time that a function will not fail, it's nice to change the type definition in that case so the user doesn't have to try to handle errors that can't happen. This usually applies to arguments where the most common pattern is to pass in a literal, such as the "unit" argument. However, even if we did that, the timestamp conversion itself can usually fail. There is a single edge case where it is known that nanoseconds is hard-coded as the unit, that this function is infallible. If you want to try to handle that case, I can give you an example to follow, however that seems like it's a small enough edge case that it might not really be helpful to users and might just end up confusing them more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this is very helpful! Regarding the nanoseconds edge case, I am inclined to agree it won't really help users.

@pront pront changed the title feat: add from_unix_timestamp function (#188) feat: add from_unix_timestamp function Jun 26, 2023
@pront pront force-pushed the pavlosr/i188-from-unix-timestamp branch from b897728 to e7f2bff Compare June 26, 2023 19:12
@pront pront marked this pull request as ready for review June 26, 2023 19:17
@fuchsnj
Copy link
Member

fuchsnj commented Jun 27, 2023

The code changes look good now. Please add a line to CHANGELOG.md in the "unreleased" section for these changes.

@pront pront merged commit 5739fdd into main Jun 27, 2023
8 checks passed
@pront pront deleted the pavlosr/i188-from-unix-timestamp branch June 27, 2023 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add from_unix_timestamp function
2 participants