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

resources/resource: Fix GroupByParamDate with raw TOML dates #11706

Merged
merged 1 commit into from
Nov 22, 2023

Conversation

jmooring
Copy link
Member

Closes #11563

@jmooring jmooring force-pushed the fix/group-by-param-date-11563 branch from e07eca7 to 9412904 Compare November 16, 2023 19:53
@@ -55,6 +58,11 @@ func getParam(r Resource, key string, stringToLower bool) any {
return cast.ToFloat64(v)
case time.Time:
return val
case toml.LocalDate, toml.LocalDateTime:
if vt, ok := hreflect.AsTime(reflect.ValueOf(val), time.UTC); ok {
Copy link
Member

Choose a reason for hiding this comment

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

Using reflect here isn't needed. You can just do val.AsTime(time.UTC).

Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't quite figure out how to do what you describe, but hopefully using htime.ToTimeInDefaultLocationE(val, time.UTC) is sufficient.

@jmooring jmooring force-pushed the fix/group-by-param-date-11563 branch from 9412904 to 442acb0 Compare November 16, 2023 22:31
@@ -55,6 +57,12 @@ func getParam(r Resource, key string, stringToLower bool) any {
return cast.ToFloat64(v)
case time.Time:
return val
case toml.LocalDate, toml.LocalDateTime:
val, err := htime.ToTimeInDefaultLocationE(val, time.UTC)
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 nit picking, but but when you have a known type, converting it to another type (e.g. interface{} or reflect.Value) just so it can be converted back to the type you started with. I don't think it allocates any extra memory in this particular case, but I had to navigate into the ToTime... func to see what it does. I suggest you just call val.AsTime directly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry about the iterations here. This was the self-inflicted wound:

case toml.LocalDate, toml.LocalDateTime:

https://go.dev/ref/spec#Type_switches

In clauses with a case listing exactly one type, the variable has that type; otherwise, the variable has the type of the expression in the TypeSwitchGuard.

Which meant I had type any instead of the toml.Whatever type, which meant AsTime() was not an option. Split the case; all is good.

@jmooring jmooring force-pushed the fix/group-by-param-date-11563 branch from 442acb0 to 7fe5100 Compare November 17, 2023 19:19
@bep bep merged commit dd6cd62 into gohugoio:master Nov 22, 2023
@jmooring jmooring deleted the fix/group-by-param-date-11563 branch November 22, 2023 17:37
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

With unquoted TOML values, GroupByParamDate requires fully qualified date
2 participants