MCPcopy Create free account
hub / github.com/buke/quickjs-go / createFieldGetter

Function createFieldGetter

class_reflect.go:374–392  ·  view source on GitHub ↗

createFieldGetter creates a getter function for a struct field

(field reflect.StructField, fieldIndex int)

Source from the content-addressed store, hash-verified

372
373// createFieldGetter creates a getter function for a struct field
374func createFieldGetter(field reflect.StructField, fieldIndex int) ClassGetterFunc {
375 return func(ctx *Context, this *Value) *Value {
376 objValue, err := getValidObjectValue(ctx, this)
377 if err != nil {
378 return ctx.ThrowError(err)
379 }
380
381 // In our implementation, fieldIndex is always valid since it comes from
382 // the loop in addReflectionAccessors, and fieldValue is always valid
383 // for exported fields that we bind
384 fieldValue := objValue.Field(fieldIndex)
385
386 jsValue, err := ctx.Marshal(fieldValue.Interface())
387 if err != nil {
388 return ctx.ThrowError(fmt.Errorf("failed to marshal field %s: %w", field.Name, err))
389 }
390 return jsValue
391 }
392}
393
394// createFieldSetter creates a setter function for a struct field
395func createFieldSetter(field reflect.StructField, fieldIndex int) ClassSetterFunc {

Callers 1

addReflectionAccessorsFunction · 0.85

Calls 3

getValidObjectValueFunction · 0.85
ThrowErrorMethod · 0.80
MarshalMethod · 0.80

Tested by

no test coverage detected