@@ -20,15 +20,19 @@ type CommandPreparation struct {
2020 CustomCommandName string
2121}
2222
23+ type CDPCommandSchema struct {
24+ Params map [string ]any
25+ Result map [string ]any
26+ }
27+
2328type CDPTypes struct {
24- CustomCommands map [string ]CustomCommand
25- CustomEvents map [string ]CustomEvent
26- CustomMiddlewares []CustomMiddleware
27- commandParamsSchemas map [string ]map [string ]any
28- commandResultSchemas map [string ]map [string ]any
29- nativeCommandSchemas map [string ]map [string ]any
30- eventSchemas map [string ]map [string ]any
31- mu sync.RWMutex
29+ CustomCommands map [string ]CustomCommand
30+ CustomEvents map [string ]CustomEvent
31+ CustomMiddlewares []CustomMiddleware
32+ commandSchemas map [string ]CDPCommandSchema
33+ nativeCommands map [string ]bool
34+ eventSchemas map [string ]map [string ]any
35+ mu sync.RWMutex
3236}
3337
3438var jsonSchemaObject = map [string ]any {"type" : "object" }
@@ -330,18 +334,17 @@ var defaultBuiltinEvents = []CustomEvent{
330334
331335func NewCDPTypes (customCommands []CustomCommand , customEvents []CustomEvent , customMiddlewares []CustomMiddleware ) * CDPTypes {
332336 types := & CDPTypes {
333- CustomCommands : map [string ]CustomCommand {},
334- CustomEvents : map [string ]CustomEvent {},
335- CustomMiddlewares : []CustomMiddleware {},
336- commandParamsSchemas : map [string ]map [string ]any {},
337- commandResultSchemas : map [string ]map [string ]any {},
338- nativeCommandSchemas : map [string ]map [string ]any {},
339- eventSchemas : map [string ]map [string ]any {},
337+ CustomCommands : map [string ]CustomCommand {},
338+ CustomEvents : map [string ]CustomEvent {},
339+ CustomMiddlewares : []CustomMiddleware {},
340+ commandSchemas : map [string ]CDPCommandSchema {},
341+ nativeCommands : map [string ]bool {},
342+ eventSchemas : map [string ]map [string ]any {},
340343 }
341344 types .hydrateNativeProtocolSchemas ()
342345 types .mu .Lock ()
343- for method , schema := range types .commandParamsSchemas {
344- types .nativeCommandSchemas [method ] = schema
346+ for method := range types .commandSchemas {
347+ types .nativeCommands [method ] = true
345348 }
346349 types .mu .Unlock ()
347350 for _ , command := range defaultBuiltinCommands {
@@ -406,12 +409,11 @@ func (types *CDPTypes) ToJSON() map[string]any {
406409 }
407410 types .mu .RLock ()
408411 state := map [string ]any {
409- "custom_commands" : len (types .CustomCommands ),
410- "custom_events" : len (types .CustomEvents ),
411- "custom_middlewares" : len (types .CustomMiddlewares ),
412- "command_params_schemas" : len (types .commandParamsSchemas ),
413- "command_result_schemas" : len (types .commandResultSchemas ),
414- "event_schemas" : len (types .eventSchemas ),
412+ "custom_commands" : len (types .CustomCommands ),
413+ "custom_events" : len (types .CustomEvents ),
414+ "custom_middlewares" : len (types .CustomMiddlewares ),
415+ "command_schemas" : len (types .commandSchemas ),
416+ "event_schemas" : len (types .eventSchemas ),
415417 }
416418 types .mu .RUnlock ()
417419 return modtypes .ModCDPToJSON (types , modtypes.ModCDPJSONConfig {
@@ -478,10 +480,8 @@ func (types *CDPTypes) PrepareCommand(method string, params map[string]any, canR
478480}
479481
480482func (types * CDPTypes ) ParseCommandParams (method string , params map [string ]any ) (map [string ]any , error ) {
481- types .mu .RLock ()
482- schema := types .commandParamsSchemas [method ]
483- types .mu .RUnlock ()
484- if schema == nil {
483+ schema , ok := types .CommandParamsSchema (method )
484+ if ! ok {
485485 return params , nil
486486 }
487487 if err := abxjsonschema .Validate (schema , params ); err != nil {
@@ -493,14 +493,49 @@ func (types *CDPTypes) ParseCommandParams(method string, params map[string]any)
493493func (types * CDPTypes ) NativeCommandSchema (method string ) map [string ]any {
494494 types .mu .RLock ()
495495 defer types .mu .RUnlock ()
496- return types .nativeCommandSchemas [method ]
496+ if ! types .nativeCommands [method ] {
497+ return nil
498+ }
499+ schema , ok := types .commandSchemas [method ]
500+ if ! ok {
501+ return nil
502+ }
503+ return map [string ]any {"params" : schema .Params , "result" : schema .Result }
497504}
498505
499- func (types * CDPTypes ) ParseCommandResult (method string , result any ) (any , error ) {
506+ func (types * CDPTypes ) CommandParamsSchema (method string ) (map [ string ] any , bool ) {
500507 types .mu .RLock ()
501- schema := types .commandResultSchemas [method ]
502- types .mu .RUnlock ()
503- if schema == nil {
508+ defer types .mu .RUnlock ()
509+ schema , ok := types .commandSchemas [method ]
510+ if ! ok || schema .Params == nil {
511+ return nil , false
512+ }
513+ return schema .Params , true
514+ }
515+
516+ func (types * CDPTypes ) CommandResultSchema (method string ) (map [string ]any , bool ) {
517+ types .mu .RLock ()
518+ defer types .mu .RUnlock ()
519+ schema , ok := types .commandSchemas [method ]
520+ if ! ok || schema .Result == nil {
521+ return nil , false
522+ }
523+ return schema .Result , true
524+ }
525+
526+ func (types * CDPTypes ) EventPayloadSchema (event string ) (map [string ]any , bool ) {
527+ types .mu .RLock ()
528+ defer types .mu .RUnlock ()
529+ schema , ok := types .eventSchemas [event ]
530+ if ! ok || schema == nil {
531+ return nil , false
532+ }
533+ return schema , true
534+ }
535+
536+ func (types * CDPTypes ) ParseCommandResult (method string , result any ) (any , error ) {
537+ schema , ok := types .CommandResultSchema (method )
538+ if ! ok {
504539 return result , nil
505540 }
506541 if err := abxjsonschema .Validate (schema , result ); err != nil {
@@ -510,10 +545,8 @@ func (types *CDPTypes) ParseCommandResult(method string, result any) (any, error
510545}
511546
512547func (types * CDPTypes ) ParseEventPayload (event string , payload any ) (any , bool ) {
513- types .mu .RLock ()
514- schema := types .eventSchemas [event ]
515- types .mu .RUnlock ()
516- if schema == nil {
548+ schema , ok := types .EventPayloadSchema (event )
549+ if ! ok {
517550 return payload , true
518551 }
519552 if err := abxjsonschema .Validate (schema , payload ); err != nil {
@@ -536,16 +569,18 @@ func (types *CDPTypes) AddCustomCommand(command CustomCommand) (string, bool, er
536569 }
537570 types .mu .Lock ()
538571 defer types .mu .Unlock ()
572+ existing := types .commandSchemas [name ]
539573 if command .ParamsSchema != nil {
540574 if schema := cloneSchema (command .ParamsSchema ); schema != nil {
541- types . commandParamsSchemas [ name ] = schema
575+ existing . Params = schema
542576 }
543577 }
544578 if command .ResultSchema != nil {
545579 if schema := cloneSchema (command .ResultSchema ); schema != nil {
546- types . commandResultSchemas [ name ] = schema
580+ existing . Result = schema
547581 }
548582 }
583+ types .commandSchemas [name ] = existing
549584 command .Name = name
550585 if command .ParamsSchema != nil {
551586 command .ParamsSchema = cloneSchema (command .ParamsSchema )
0 commit comments