Refactor authorized object handling in JS API
- Updated the NewObject method to pass the complete authorized structure instead of individual fields, simplifying the code and improving clarity. - Enhanced handling for nil authorized scenarios by setting an empty object, ensuring consistent behavior in context management. - Released Go-side persistent handles for authorized and empty objects to maintain proper resource management.
This commit is contained in:
parent
53a22fd9e4
commit
d9b8496a7d
1 changed files with 11 additions and 12 deletions
|
|
@ -113,21 +113,20 @@ func (ctx *Context) NewObject(v8ctx *v8go.Context) (*v8go.Value, error) {
|
|||
metadataVal.Release() // Release Go-side Persistent handle, V8 internal reference remains
|
||||
}
|
||||
|
||||
// Authorized object - set individual fields to ensure proper structure
|
||||
var authorizedData map[string]interface{}
|
||||
// Authorized object - pass the complete structure
|
||||
if ctx.Authorized != nil {
|
||||
authorizedData = map[string]interface{}{
|
||||
"user_id": ctx.Authorized.UserID,
|
||||
"tenant_id": ctx.Authorized.TenantID,
|
||||
"client_id": ctx.Authorized.ClientID,
|
||||
authorizedVal, err := bridge.JsValue(v8ctx, ctx.Authorized)
|
||||
if err == nil {
|
||||
obj.Set("authorized", authorizedVal)
|
||||
authorizedVal.Release() // Release Go-side Persistent handle, V8 internal reference remains
|
||||
}
|
||||
} else {
|
||||
authorizedData = map[string]interface{}{}
|
||||
}
|
||||
authorizedVal, err := bridge.JsValue(v8ctx, authorizedData)
|
||||
if err == nil {
|
||||
obj.Set("authorized", authorizedVal)
|
||||
authorizedVal.Release() // Release Go-side Persistent handle, V8 internal reference remains
|
||||
// Set to empty object when nil
|
||||
emptyObj, err := bridge.JsValue(v8ctx, map[string]interface{}{})
|
||||
if err == nil {
|
||||
obj.Set("authorized", emptyObj)
|
||||
emptyObj.Release()
|
||||
}
|
||||
}
|
||||
|
||||
return instance.Value, nil
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue