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

Function initializeFromPositionalArgs

class_reflect.go:187–214  ·  view source on GitHub ↗

initializeFromPositionalArgs initializes struct fields from positional arguments

(instance interface{}, args []*Value, typ reflect.Type, ctx *Context)

Source from the content-addressed store, hash-verified

185
186// initializeFromPositionalArgs initializes struct fields from positional arguments
187func initializeFromPositionalArgs(instance interface{}, args []*Value, typ reflect.Type, ctx *Context) error {
188 val := reflect.ValueOf(instance).Elem() // Dereference pointer to get struct value
189
190 argIndex := 0
191 for i := 0; i < typ.NumField(); i++ {
192 field := typ.Field(i)
193
194 // Skip unexported fields
195 if !field.IsExported() {
196 continue
197 }
198
199 // Break if no more arguments
200 if argIndex >= len(args) {
201 break
202 }
203
204 fieldValue := val.Field(i)
205 if fieldValue.CanSet() {
206 if err := ctx.unmarshal(args[argIndex], fieldValue); err != nil {
207 return fmt.Errorf("failed to set field %s: %w", field.Name, err)
208 }
209 argIndex++
210 }
211 }
212
213 return nil
214}
215
216// initializeFromObjectArgs initializes struct fields from object properties (named parameters)
217func initializeFromObjectArgs(instance interface{}, obj *Value, typ reflect.Type, ctx *Context) error {

Callers 1

initializeFromArgsFunction · 0.85

Calls 1

unmarshalMethod · 0.80

Tested by

no test coverage detected