(1) Background
To fix following issue:
I implemented a new contains
operator in Hugo where
function in following PR:
(2) How Hugo where function works
Basically Hugo where
function use the checkCondition()
function to check if the given field value matches the match value or not. The complete codes in: "/tpl/collections/where.go"
v
: given field value, represents as reflect.Valuemv
: match value, also reprsents as reflect.Valueivp
: the pointer of integer type of given filed valueimvp
: the pointer of integer type of match valuesvp
: the pointer of string type of give filed valuesmvp
: the pointer of string type of match valueiva
: a interger array converts from a given field valuesva
: a strign array converts from a given field value
- Firstly, it gets the real value it points to if v or mv is a pointer or interface,
indirect()
defined in "./tpl/collections/apply.go" - if it can deference the
v
ormv
, compare them with those basic operators - if it detected that
v
ormv
is boolean value, just compare them - convert given field value and match value and stores to correpsonding varaibles
- based on variables it generates, execute operate corresponding logic
Here, I take In
operation as an example. when given field is basic type, e,g: string, and match value is a slice, it does below logic:
- check if match value is a slice or array, if not, return false
- if match value is zero length, return false
- if child item’s type in match value(now it is array or slice) doesn’t match the given field value’s type, return false
- convert given field value and storge information into correspdoning variables
- if given field value is a interger, then
ivp
is not nil, and by logic above, the match value is a interger slice, so a interger arrayiva
build from match value slice should be generated already,
use functionIn
to compare the given field value in match value array or not - if given field value is a string, then
svp
is not nil, here match value can be a string or string list, compare useIn
function too
(3) New contains
operator implementaion logic
- Make sure given field value is either Slice, Array or String and try to converts given field value information to
ivp
,svp
,slv
- Make sure given field value length is larger than 0
- If given field value is Slice or Array, then make sure the child item of given field is the same type as the match value
- Converts the match value and store nessary information to
imvp
,smvp
,slmv
, - If given field value is Slice or Array and child item is TimeType, try to converts the given field value and match value to
ima
andiva
, in other words, converts the timeType to int64 array - Finally, evaluate the contaings logic with below logic:
- if
iva
is not nil which means the given field is an interger array, check ifiva
(interger array of give value) containsimvp
(interger match value) - if
smvp
is not nil which means the match value is a string, then check below logic - if
sva
length larger than 0 which means given field is a string array, check ifsva
(string array of given value) containssmvp
(string match value) - else if
sva
is 0 butsvp
is not nil, which means given field is a string, check ifspv
(string of given value) contains substringsmvp
(string match value) - if
slv
andslmv
is not nil, which means given value and match value are all either Slice or Array, then check below logic: - check if
iva
larger than 0, which means given value can converts to a timeType array, then we check if each item inima
(interger array of match value) are all iniva
(interger array of give value), return true if all contains in given value, return false is it fails - above logic should returns if the child item in give value array is timeType, now for other types of child item, we just check with the Hugo
In
function
- if
(4) How to apply the patch
-
Firstly, create the corresponding build path for hugo project
# mkdir -p /path/to/GOROOT/src/github.com/gohugoio/ # cd /path/to/GOROOT/src/github.com/gohugoio/ # git clone https://github.com/gohugoio/hugo.git
-
Switch to the release you want to apply the patch, e,g: release-0.37
# git checkout release-0.37
-
Download the patch: https://patch-diff.githubusercontent.com/raw/gohugoio/hugo/pull/4132.patch and put into git project dir
# git apply --stat 4132.patch # git apply --check 4132.patch # git am --signoff < 4132.patch
-
Download and get
mage
compile tool ready, build hugo binary# go get github.com/magefile/mage # mage vendor # mage hugo
-
The builded binary
hugo
file can be found in /path/to/GOROOT/src/github.com/gohugoio/hugo/hugo