-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
where
does not handle template.HTML and string comparisons
#8353
Comments
Which fails to produce the expected results? I am unable to find problems with either with a simple test. |
There are two loops. One using where and the other using if (eq) to compare the same path. |
Found something interesting in func (ns *Namespace) checkWhereArray(seqv, kv, mv reflect.Value, path []string, op string) (interface{}, error) {
rv := reflect.MakeSlice(seqv.Type(), 0, 0)
for i := 0; i < seqv.Len(); i++ {
var vvv reflect.Value
rvv := seqv.Index(i)
if kv.Kind() == reflect.String {
if params, ok := rvv.Interface().(maps.Params); ok {
vvv = reflect.ValueOf(params.Get(path...))
} else {
vvv = rvv
for _, elemName := range path {
var err error
vvv, err = evaluateSubElem(vvv, elemName)
if err != nil {
// THIS CONTINUE SHOULD REALLY CONTINUE THE OUTER LOOP
// SO PROBABLY NEEDS SOME KIND OF ESCAPE JUST INCASE mv is null
continue
}
}
}
} else {
vv, _ := indirect(rvv)
if vv.Kind() == reflect.Map && kv.Type().AssignableTo(vv.Type().Key()) {
vvv = vv.MapIndex(kv)
}
}
if ok, err := ns.checkCondition(vvv, mv, op); ok {
rv = reflect.Append(rv, rvv)
} else if err != nil {
return nil, err
}
}
return rv.Interface(), nil
} But although the continue should be a break and then some kind of if nil continue outside of the path loop, this still would not explain the problem. Is is possible that somewhere along the way properties with the signature "name" are treated specially? Really don't know but when tracing through checkWhereArray I do not get a hit in checkCondition with path containing "Params", "name" |
@richtera, can you provide a simple demo site the reproduces this issue? |
Line 90 in 9793477
|
I can reproduce the behavior by:
In this contrived example, When I use Again, my failing test case is contrived, but I'm curious...
|
Params.name is a string from yaml in a md frontmatter. (The snippet is running on a different page (a related page which is linked by the shortUrl)
My RelPermalinks have 5 segments and $shortUrl only the last two. Since they are both strings it should have matched correctly, but I guess they are of different string Type() but the same Kind(). BTWL the image of the debugger of the two values further up in this thread shows that both use runtime.strequal for the comparator. |
Produces:
About a year ago the last comparison (using the So I guess we need to do the same thing with |
in
does not work with template.HTML strings
in
does not work with template.HTML stringswhere
does not work with template.HTML strings
@jmooring Other than delimit, I do use printf to construct query strings for where; that might be another candidate if changing to comparing Kind() is not feasible. |
The underlying issue is that we don't handle the case of comparing arrays and slices in We probably need to pull out the core of the operations functions from |
where
does not work with template.HTML stringswhere
does not test arrays or slices for all operations
But my use case is a string to string comparison. There is no slice or duct involved. Hmmm. |
@bep I am still concerned that this ticket got changed to talking about arrays and slices although my use case was using plain string comparison and failed because mv.Type() == v.Type() would return false and end up in the array/slice part of the comparison code. |
Sorry, my research is based upon @jmooring's test site, whish may be presenting a slightly different use case. Do you have a demo site the reproduces your issue? |
I can create one. I just traced through the code and see the cause of the problem. Changing from Type() to Kind() makes everything work correctly. Ok I'll put one together. |
https://github.com/getselfstudy/hugo-where-failure <h1>TEST</h1>
<ul>
<li>Found /post/another-post/</li>
</ul>
<h1>TEST2</h1>
<ul>
</ul> |
Sorry for the confusion. You're right about the The Type comparison in |
where
does not test arrays or slices for all operationswhere
does not handle template.HTML and string comparisons
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Can be reproduced on latest, but I can't quite explain it.
I have these two range loops in my code and one works and the other doesn't.
It seems I only get outputs containing HasMatch1. Ideally, both should end up with the same items. What's curious is that most of the time "where" is working as expected and not causing problems. So I am not quite sure what's going on or how to debug it yet.
The text was updated successfully, but these errors were encountered: