Debian Codegraph @

common/collections/append.go

raw
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 fromv.Len() == 0 {
86 return tov.Interface(), nil
87 }
88
89 // If we get []string []string, we append the from slice to to
90 if tot == fromt {
91 return reflect.AppendSlice(tov, fromv).Interface(), nil
92 } else if !fromt.AssignableTo(tot) {
93 // Fall back to a []interface{} slice.
94 return appendToInterfaceSliceFromValues(tov, fromv)
95 }
96
97 }
98 }
99 }
100
101 if toIsNil {
102 return Slice(from...), nil
103 }
104
105 for _, f := range from {
106 fv := reflect.ValueOf(f)
107 if !fv.IsValid() || !fv.Type().AssignableTo(tot) {
108 // Fall back to a []interface{} slice.
109 tov, _ := hreflect.Indirect(reflect.ValueOf(to))
110 return appendToInterfaceSlice(tov, from...)
111 }
112 tov = reflect.Append(tov, fv)
113 }
114
115 return tov.Interface(), nil
116 }
117
118 func appendToInterfaceSliceFromValues(slice1, slice2 reflect.Value) ([]any, error) {
119 var tos []any
120
121 for _, slice := range []reflect.Value{slice1, slice2} {
122 if !slice.IsValid() {
123 tos = append(tos, nil)
124 continue
125 }
126 for i := range slice.Len() {
127 tos = append(tos, slice.Index(i).Interface())
128 }
129 }
130
131 return tos, nil
132 }
133
134 func appendToInterfaceSlice(tov reflect.Value, from ...any) ([]any, error) {
135 var tos []any
136
137 for i := range tov.Len() {
138 tos = append(tos, tov.Index(i).Interface())
139 }
140
141 tos = append(tos, from...)
142
143 return tos, nil
144 }