templates: permit sorting by struct key #1647
Merged
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.
As suggested in #dev-discussion, permit the
sort
template function to index (exported) struct fields, so that code likejust works, instead of erroring with
cannot index value of type discordgo.Role
.Note that the proposed patch still does not support niladic method calls. Hence, code like
{{ sort $sliceOfTimes (sdict "key" "Hour") }}
remains an error, sinceHour
is a method ontime.Time
, not a field. Ideally, we would lift this restriction and completely align the implementation of thekey
option with field access in templates, such that{{ sort $xs (sdict "key" "K") }}
corresponds to sorting by$x.K
. This behavior, however, is much more difficult to implement so is not done in this PR (and it is debatable whether it is worth the added complexity.)