| 1 |
// Copyright 2019 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 |
"fmt" |
| 18 |
"reflect" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/common/hreflect" |
| 21 |
) |
| 22 |
|
| 23 |
// Append appends from to a slice to and returns the resulting slice. |
| 24 |
// If length of from is one and the only element is a slice of same type as to, |
| 25 |
// it will be appended. |
| 26 |
func Append(to any, from ...any) (any, error) { |
| 27 |
if len(from) == 0 { |
| 28 |
return to, nil |
| 29 |
} |
| 30 |
tov, toIsNil := hreflect.Indirect(reflect.ValueOf(to)) |
| 31 |
|
| 32 |
toIsNil = toIsNil || to == nil |
| 33 |
var tot reflect.Type |
| 34 |
|
| 35 |
if !toIsNil { |
| 36 |
if tov.Kind() == reflect.Slice { |
| 37 |
// Create a copy of tov, so we don't modify the original. |
| 38 |
c := reflect.MakeSlice(tov.Type(), tov.Len(), tov.Len()+len(from)) |
| 39 |
reflect.Copy(c, tov) |
| 40 |
tov = c |
| 41 |
} |
| 42 |
|
| 43 |
if tov.Kind() != reflect.Slice { |
| 44 |
return nil, fmt.Errorf("expected a slice, got %T", to) |
| 45 |
} |
| 46 |
|
| 47 |
tot = tov.Type().Elem() |
| 48 |
if tot.Kind() == reflect.Slice { |
| 49 |
totvt := tot.Elem() |
| 50 |
fromvs := make([]reflect.Value, len(from)) |
| 51 |
for i, f := range from { |
| 52 |
fromv := reflect.ValueOf(f) |
| 53 |
fromt := fromv.Type() |
| 54 |
if fromt.Kind() == reflect.Slice { |
| 55 |
fromt = fromt.Elem() |
| 56 |
} |
| 57 |
if totvt != fromt { |
| 58 |
return nil, fmt.Errorf("cannot append slice of %s to slice of %s", fromt, totvt) |
| 59 |
} else { |
| 60 |
fromvs[i] = fromv |
| 61 |
} |
| 62 |
} |
| 63 |
return reflect.Append(tov, fromvs...).Interface(), nil |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
toIsNil = tov.Len() == 0 |
| 68 |
|
| 69 |
if len(from) == 1 { |
| 70 |
fromv := reflect.ValueOf(from[0]) |
| 71 |
if !fromv.IsValid() { |
| 72 |
// from[0] is nil |
| 73 |
return appendToInterfaceSliceFromValues(tov, fromv) |
| 74 |
} |
| 75 |
fromt := fromv.Type() |
| 76 |
if fromt.Kind() == reflect.Slice { |
| 77 |
fromt = fromt.Elem() |
| 78 |
} |
| 79 |
if fromv.Kind() == reflect.Slice { |
| 80 |
if toIsNil { |
| 81 |
// If we get nil []string, we just return the []string |
| 82 |
return from[0], nil |
| 83 |
} |
| 84 |
|
| 85 |
// If we get []string []string, we append the from slice to to |
| 86 |
if tot == fromt { |
| 87 |
return reflect.AppendSlice(tov, fromv).Interface(), nil |
| 88 |
} else if !fromt.AssignableTo(tot) { |
| 89 |
// Fall back to a []interface{} slice. |
| 90 |
return appendToInterfaceSliceFromValues(tov, fromv) |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if toIsNil { |
| 98 |
return Slice(from...), nil |
| 99 |
} |
| 100 |
|
| 101 |
for _, f := range from { |
| 102 |
fv := reflect.ValueOf(f) |
| 103 |
if !fv.IsValid() || !fv.Type().AssignableTo(tot) { |
| 104 |
// Fall back to a []interface{} slice. |
| 105 |
tov, _ := hreflect.Indirect(reflect.ValueOf(to)) |
| 106 |
return appendToInterfaceSlice(tov, from...) |
| 107 |
} |
| 108 |
tov = reflect.Append(tov, fv) |
| 109 |
} |
| 110 |
|
| 111 |
return tov.Interface(), nil |
| 112 |
} |
| 113 |
|
| 114 |
func appendToInterfaceSliceFromValues(slice1, slice2 reflect.Value) ([]any, error) { |
| 115 |
var tos []any |
| 116 |
|
| 117 |
for _, slice := range []reflect.Value{slice1, slice2} { |
| 118 |
if !slice.IsValid() { |
| 119 |
tos = append(tos, nil) |
| 120 |
continue |
| 121 |
} |
| 122 |
for i := range slice.Len() { |
| 123 |
tos = append(tos, slice.Index(i).Interface()) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return tos, nil |
| 128 |
} |
| 129 |
|
| 130 |
func appendToInterfaceSlice(tov reflect.Value, from ...any) ([]any, error) { |
| 131 |
var tos []any |
| 132 |
|
| 133 |
for i := range tov.Len() { |
| 134 |
tos = append(tos, tov.Index(i).Interface()) |
| 135 |
} |
| 136 |
|
| 137 |
tos = append(tos, from...) |
| 138 |
|
| 139 |
return tos, nil |
| 140 |
} |