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

Function initializeFromObjectArgs

class_reflect.go:217–249  ·  view source on GitHub ↗

initializeFromObjectArgs initializes struct fields from object properties (named parameters)

(instance interface{}, obj *Value, typ reflect.Type, ctx *Context)

Source from the content-addressed store, hash-verified

215
216// initializeFromObjectArgs initializes struct fields from object properties (named parameters)
217func initializeFromObjectArgs(instance interface{}, obj *Value, typ reflect.Type, ctx *Context) error {
218 val := reflect.ValueOf(instance).Elem()
219
220 for i := 0; i < typ.NumField(); i++ {
221 field := typ.Field(i)
222
223 // Skip unexported fields
224 if !field.IsExported() {
225 continue
226 }
227
228 // Use unified tag parsing from marshal.go
229 tagInfo := parseFieldTag(field)
230 if tagInfo.Skip {
231 continue // Field marked with "-" tag
232 }
233
234 // Check if object has this property and set field value
235 if obj.Has(tagInfo.Name) {
236 propValue := obj.Get(tagInfo.Name)
237 defer propValue.Free()
238
239 fieldValue := val.Field(i)
240 if fieldValue.CanSet() {
241 if err := ctx.unmarshal(propValue, fieldValue); err != nil {
242 return fmt.Errorf("failed to set field %s from property %s: %w", field.Name, tagInfo.Name, err)
243 }
244 }
245 }
246 }
247
248 return nil
249}
250
251// addReflectionMethods scans struct methods and adds them to the ClassBuilder
252// Only exported methods are processed due to Go reflection limitations

Callers 1

initializeFromArgsFunction · 0.85

Calls 5

parseFieldTagFunction · 0.85
HasMethod · 0.80
GetMethod · 0.80
unmarshalMethod · 0.80
FreeMethod · 0.45

Tested by

no test coverage detected