SetSlice sets the source slice that we are viewing.
(sl any)
| 122 | |
| 123 | // SetSlice sets the source slice that we are viewing. |
| 124 | func (tb *Table) SetSlice(sl any) *Table { |
| 125 | if reflectx.AnyIsNil(sl) { |
| 126 | tb.Slice = nil |
| 127 | return tb |
| 128 | } |
| 129 | if tb.Slice == sl { |
| 130 | tb.MakeIter = 0 |
| 131 | return tb |
| 132 | } |
| 133 | |
| 134 | slpTyp := reflect.TypeOf(sl) |
| 135 | if slpTyp.Kind() != reflect.Pointer { |
| 136 | slog.Error("Table requires that you pass a pointer to a slice of struct elements, but type is not a Ptr", "type", slpTyp) |
| 137 | return tb |
| 138 | } |
| 139 | if slpTyp.Elem().Kind() != reflect.Slice { |
| 140 | slog.Error("Table requires that you pass a pointer to a slice of struct elements, but ptr doesn't point to a slice", "type", slpTyp.Elem()) |
| 141 | return tb |
| 142 | } |
| 143 | eltyp := reflectx.NonPointerType(reflectx.SliceElementType(sl)) |
| 144 | if eltyp.Kind() != reflect.Struct { |
| 145 | slog.Error("Table requires that you pass a slice of struct elements, but type is not a Struct", "type", eltyp.String()) |
| 146 | return tb |
| 147 | } |
| 148 | |
| 149 | tb.SetSliceBase() |
| 150 | tb.Slice = sl |
| 151 | tb.sliceUnderlying = reflectx.Underlying(reflect.ValueOf(tb.Slice)) |
| 152 | tb.elementValue = reflectx.Underlying(reflectx.SliceElementValue(sl)) |
| 153 | tb.cacheVisibleFields() |
| 154 | return tb |
| 155 | } |
| 156 | |
| 157 | // cacheVisibleFields caches the visible struct fields. |
| 158 | func (tb *Table) cacheVisibleFields() { |