|
| 1 | +package etk |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + |
| 6 | + "src.elv.sh/pkg/eval" |
| 7 | + "src.elv.sh/pkg/eval/vals" |
| 8 | + "src.elv.sh/pkg/must" |
| 9 | +) |
| 10 | + |
| 11 | +// A slightly nicer wrapper of scanToGo. |
| 12 | +func ScanToGo[T any](val any, fm *eval.Frame) (T, error) { |
| 13 | + var dst T |
| 14 | + err := scanToGo(val, &dst, fm) |
| 15 | + if err == nil { |
| 16 | + return dst, nil |
| 17 | + } |
| 18 | + return zero[T](), err |
| 19 | +} |
| 20 | + |
| 21 | +// A variant of vals.ScanToGo, |
| 22 | +// with additional support for adapting an Elvish function to a Go function, |
| 23 | +// or an Elvish map to a Go interface. |
| 24 | +func scanToGo(val, ptr any, fm *eval.Frame) error { |
| 25 | + err := vals.ScanToGo(val, ptr) |
| 26 | + |
| 27 | + dst := reflect.ValueOf(ptr).Elem() |
| 28 | + dstType := reflect.TypeOf(ptr).Elem() |
| 29 | + if fn, ok := val.(eval.Callable); ok && dstType.Kind() == reflect.Func { |
| 30 | + // Adapt an Elvish function to a Go function |
| 31 | + dst.Set(reflect.MakeFunc(dstType, func(args []reflect.Value) []reflect.Value { |
| 32 | + // TODO: Handle errors properly |
| 33 | + // TODO: Add intermediate "internal" entry to the traceback |
| 34 | + outs := must.OK1(fm.CaptureOutput(func(fm *eval.Frame) error { |
| 35 | + return fn.Call(fm, each(args, reflect.Value.Interface), eval.NoOpts) |
| 36 | + })) |
| 37 | + goOuts := make([]reflect.Value, dstType.NumOut()) |
| 38 | + if len(outs) != len(goOuts) { |
| 39 | + panic("wrong number of outputs") |
| 40 | + } |
| 41 | + for i, out := range outs { |
| 42 | + goOutPtr := reflect.New(dstType.Out(i)) |
| 43 | + must.OK(scanToGo(out, goOutPtr.Interface(), fm)) |
| 44 | + goOuts[i] = reflect.Indirect(goOutPtr) |
| 45 | + } |
| 46 | + return goOuts |
| 47 | + })) |
| 48 | + return nil |
| 49 | + } else if structType, ok := structOfFuncForInterface[dstType]; ok { |
| 50 | + // Scan an Elvish map into a Go interface. |
| 51 | + if _, ok := val.(vals.Map); ok { |
| 52 | + // TODO: Accept other map-like types too |
| 53 | + // Create a zero value of the struct type, and fill all of its |
| 54 | + // fields from the map. |
| 55 | + structPtr := reflect.New(structType) |
| 56 | + err := vals.ScanFieldMapFromMap( |
| 57 | + val, structPtr.Interface(), |
| 58 | + vals.GetFieldMapKeys(structPtr.Elem().Interface()), |
| 59 | + vals.AllowExtraMapKey, |
| 60 | + func(src, ptr any, opts vals.ScanOpt) error { |
| 61 | + // TODO: Don't ignore opts |
| 62 | + return scanToGo(src, ptr, fm) |
| 63 | + }) |
| 64 | + if err == nil { |
| 65 | + dst.Set(structPtr.Elem()) |
| 66 | + return nil |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return err |
| 71 | +} |
| 72 | + |
| 73 | +// Maps an interface to its "struct of func" implementation. |
| 74 | +var structOfFuncForInterface = map[reflect.Type]reflect.Type{} |
| 75 | + |
| 76 | +// Registers a "struct of func implementation" of an interface. |
| 77 | +// |
| 78 | +// For example, the following interface: |
| 79 | +// |
| 80 | +// type I interface { |
| 81 | +// Foo(a int) |
| 82 | +// Bar() int |
| 83 | +// } |
| 84 | +// |
| 85 | +// Can be implemented by the following "struct of func": |
| 86 | +// |
| 87 | +// type S struct { |
| 88 | +// FooImpl func(a int) |
| 89 | +// BarImpl func() int |
| 90 | +// } |
| 91 | +// func (s S) Foo(a int) { s.FooImpl(a) } |
| 92 | +// func (s S) Bar() int { return s.BarImpl() } |
| 93 | +// |
| 94 | +// (Each field has the "Impl" by convention.) |
| 95 | +// |
| 96 | +// And you would call this function like this to register their relationship: |
| 97 | +// |
| 98 | +// var _ = RegisterStructOfFuncForInterface[S, I]() |
| 99 | +// |
| 100 | +// (The function has a useless return value so that it can be called from the top level.) |
| 101 | +// |
| 102 | +// This registration allows [ScanToGo] to scan an Elvish map into a Go interface, |
| 103 | +// like: |
| 104 | +// |
| 105 | +// [&foo={|a| ... } & bar={ num 1 }] |
| 106 | +func RegisterStructOfFuncForInterface[S, I any]() struct{} { |
| 107 | + stype := reflect.TypeFor[S]() |
| 108 | + itype := reflect.TypeFor[I]() |
| 109 | + if stype.Kind() != reflect.Struct { |
| 110 | + panic("S must be a struct type") |
| 111 | + } |
| 112 | + szero := reflect.Zero(stype).Interface() |
| 113 | + if !vals.IsFieldMap(szero) { |
| 114 | + panic("S must be a field map") |
| 115 | + } |
| 116 | + if itype.Kind() != reflect.Interface { |
| 117 | + panic("I must be an interface type") |
| 118 | + } |
| 119 | + if !stype.AssignableTo(itype) { |
| 120 | + panic("S must implement I") |
| 121 | + } |
| 122 | + structOfFuncForInterface[itype] = stype |
| 123 | + return struct{}{} |
| 124 | +} |
0 commit comments