MCPcopy
hub / github.com/go-qml/qml / convertAndSet

Function convertAndSet

bridge.go:435–478  ·  view source on GitHub ↗
(to, from reflect.Value, setMethod reflect.Value)

Source from the content-addressed store, hash-verified

433}
434
435func convertAndSet(to, from reflect.Value, setMethod reflect.Value) (err error) {
436 var toType reflect.Type
437 if setMethod.IsValid() {
438 toType = setMethod.Type().In(0)
439 } else {
440 toType = to.Type()
441 }
442 fromType := from.Type()
443 defer func() {
444 // TODO This is catching more than it should. There are calls
445 // to custom code below that should be isolated.
446 if v := recover(); v != nil {
447 err = fmt.Errorf("cannot use %s as a %s", fromType, toType)
448 }
449 }()
450 if fromType == typeList && toType.Kind() == reflect.Slice {
451 list := from.Interface().(*List)
452 from = reflect.MakeSlice(toType, len(list.data), len(list.data))
453 elemType := toType.Elem()
454 for i, elem := range list.data {
455 from.Index(i).Set(reflect.ValueOf(elem).Convert(elemType))
456 }
457 } else if fromType == typeMap && toType.Kind() == reflect.Map {
458 qmap := from.Interface().(*Map)
459 from = reflect.MakeMap(toType)
460 elemType := toType.Elem()
461 for i := 0; i < len(qmap.data); i += 2 {
462 key := reflect.ValueOf(qmap.data[i])
463 val := reflect.ValueOf(qmap.data[i+1])
464 if val.Type() != elemType {
465 val = val.Convert(elemType)
466 }
467 from.SetMapIndex(key, val)
468 }
469 } else if toType != fromType {
470 from = from.Convert(toType)
471 }
472 if setMethod.IsValid() {
473 setMethod.Call([]reflect.Value{from})
474 } else {
475 to.Set(from)
476 }
477 return nil
478}
479
480var (
481 dataValueSize = uintptr(unsafe.Sizeof(C.DataValue{}))

Callers 4

ConvertMethod · 0.85
ConvertMethod · 0.85
hookGoValueWriteFieldFunction · 0.85
convertParamFunction · 0.85

Calls 4

InterfaceMethod · 0.65
SetMethod · 0.65
CallMethod · 0.65
ConvertMethod · 0.45

Tested by

no test coverage detected