github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/tpl/collections/complement.go (about)

     1  // Copyright 2018 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package collections
    15  
    16  import (
    17  	"errors"
    18  	"fmt"
    19  	"reflect"
    20  )
    21  
    22  // Complement gives the elements in the last element of ls that are not in
    23  // any of the others.
    24  //
    25  // All elements of ls must be slices or arrays of comparable types.
    26  //
    27  // The reasoning behind this rather clumsy API is so we can do this in the templates:
    28  //
    29  //	{{ $c := .Pages | complement $last4 }}
    30  func (ns *Namespace) Complement(ls ...any) (any, error) {
    31  	if len(ls) < 2 {
    32  		return nil, errors.New("complement needs at least two arguments")
    33  	}
    34  
    35  	universe := ls[len(ls)-1]
    36  	as := ls[:len(ls)-1]
    37  
    38  	aset, err := collectIdentities(as...)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	v := reflect.ValueOf(universe)
    44  	switch v.Kind() {
    45  	case reflect.Array, reflect.Slice:
    46  		sl := reflect.MakeSlice(v.Type(), 0, 0)
    47  		for i := 0; i < v.Len(); i++ {
    48  			ev, _ := indirectInterface(v.Index(i))
    49  			if _, found := aset[normalize(ev)]; !found {
    50  				sl = reflect.Append(sl, ev)
    51  			}
    52  		}
    53  		return sl.Interface(), nil
    54  	default:
    55  		return nil, fmt.Errorf("arguments to complement must be slices or arrays")
    56  	}
    57  }