github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/valuer/unsafe.go (about) 1 // Copyright 2021 ecodeclub 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package valuer 16 17 import ( 18 "reflect" 19 "unsafe" 20 21 "github.com/ecodeclub/eorm/internal/rows" 22 23 "github.com/ecodeclub/eorm/internal/errs" 24 "github.com/ecodeclub/eorm/internal/model" 25 ) 26 27 var _ Creator = NewUnsafeValue 28 29 type unsafeValue struct { 30 val reflect.Value 31 addr unsafe.Pointer 32 meta *model.TableMeta 33 } 34 35 func NewUnsafeValue(val interface{}, meta *model.TableMeta) Value { 36 refVal := reflect.ValueOf(val) 37 return unsafeValue{ 38 meta: meta, 39 val: refVal.Elem(), 40 addr: unsafe.Pointer(refVal.Pointer()), 41 } 42 } 43 44 func (u unsafeValue) Field(name string) (reflect.Value, error) { 45 fd, ok := u.meta.FieldMap[name] 46 if !ok { 47 return reflect.Value{}, errs.NewInvalidFieldError(name) 48 } 49 ptr := unsafe.Pointer(uintptr(u.addr) + fd.Offset) 50 val := reflect.NewAt(fd.Typ, ptr).Elem() 51 return val, nil 52 } 53 54 func (u unsafeValue) SetColumns(rows rows.Rows) error { 55 56 cs, err := rows.Columns() 57 if err != nil { 58 return err 59 } 60 if len(cs) > len(u.meta.Columns) { 61 return errs.ErrTooManyColumns 62 } 63 64 // TODO 性能优化 65 colValues := make([]interface{}, len(cs)) 66 for i, c := range cs { 67 cm, ok := u.meta.ColumnMap[c] 68 if !ok { 69 return errs.NewInvalidColumnError(c) 70 } 71 ptr := unsafe.Pointer(uintptr(u.addr) + cm.Offset) 72 val := reflect.NewAt(cm.Typ, ptr) 73 colValues[i] = val.Interface() 74 } 75 return rows.Scan(colValues...) 76 }