Skip to content

Commit a49a405

Browse files
committed
Add generated alias client codegen
1 parent a2466b2 commit a49a405

26 files changed

Lines changed: 293923 additions & 14 deletions

go/modcdp/client/CDPTypes.go

Lines changed: 193 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type CDPTypes struct {
2929
CustomCommands map[string]CustomCommand
3030
CustomEvents map[string]CustomEvent
3131
CustomMiddlewares []CustomMiddleware
32+
CustomAliasObjects map[string]CustomAliasObject
3233
commandSchemas map[string]CDPCommandSchema
3334
commandParamsSchemas map[string]map[string]any
3435
commandResultSchemas map[string]map[string]any
@@ -105,6 +106,47 @@ var modMiddlewareRegistrationSchema = map[string]any{
105106
"additionalProperties": false,
106107
}
107108

109+
var modAliasReturnSchema = map[string]any{
110+
"type": "object",
111+
"properties": map[string]any{
112+
"object": map[string]any{"type": "string"},
113+
"unwrap": map[string]any{"type": "string"},
114+
"array": map[string]any{"type": "boolean"},
115+
"nullable": map[string]any{"type": "boolean"},
116+
},
117+
"additionalProperties": false,
118+
}
119+
120+
var modAliasMethodRegistrationSchema = map[string]any{
121+
"type": "object",
122+
"properties": map[string]any{
123+
"name": map[string]any{"type": "string"},
124+
"command": map[string]any{"type": "string"},
125+
"params_schema": map[string]any{"type": []any{"object", "null"}},
126+
"result_schema": map[string]any{"type": []any{"object", "null"}},
127+
"sticky_param": map[string]any{"type": "string"},
128+
"sticky_params": map[string]any{"type": "array", "items": map[string]any{"type": "string"}},
129+
"sticky_fields": map[string]any{"type": "array", "items": map[string]any{"type": "string"}},
130+
"return": modAliasReturnSchema,
131+
},
132+
"required": []any{"name", "command"},
133+
"additionalProperties": false,
134+
}
135+
136+
var modAliasObjectRegistrationSchema = map[string]any{
137+
"type": "object",
138+
"properties": map[string]any{
139+
"name": map[string]any{"type": "string"},
140+
"type_name": map[string]any{"type": "string"},
141+
"root": map[string]any{"type": "boolean"},
142+
"sticky_schema": map[string]any{"type": []any{"object", "null"}},
143+
"sticky_fields": map[string]any{"type": "array", "items": map[string]any{"type": "string"}},
144+
"methods": map[string]any{"type": "array", "items": modAliasMethodRegistrationSchema},
145+
},
146+
"required": []any{"name"},
147+
"additionalProperties": false,
148+
}
149+
108150
var modConfigureParamsSchema = map[string]any{
109151
"type": "object",
110152
"properties": map[string]any{
@@ -149,6 +191,7 @@ var modConfigureParamsSchema = map[string]any{
149191
"custom_commands": map[string]any{"type": "array", "items": modCommandRegistrationSchema},
150192
"custom_events": map[string]any{"type": "array", "items": modEventRegistrationSchema},
151193
"custom_middlewares": map[string]any{"type": "array", "items": modMiddlewareRegistrationSchema},
194+
"custom_alias_objects": map[string]any{"type": "array", "items": modAliasObjectRegistrationSchema},
152195
},
153196
"additionalProperties": false,
154197
}
@@ -327,11 +370,12 @@ var defaultBuiltinEvents = []CustomEvent{
327370
},
328371
}
329372

330-
func NewCDPTypes(customCommands []CustomCommand, customEvents []CustomEvent, customMiddlewares []CustomMiddleware) *CDPTypes {
373+
func NewCDPTypes(customCommands []CustomCommand, customEvents []CustomEvent, customMiddlewares []CustomMiddleware, customAliasObjectGroups ...[]CustomAliasObject) *CDPTypes {
331374
types := &CDPTypes{
332375
CustomCommands: map[string]CustomCommand{},
333376
CustomEvents: map[string]CustomEvent{},
334377
CustomMiddlewares: []CustomMiddleware{},
378+
CustomAliasObjects: map[string]CustomAliasObject{},
335379
commandSchemas: map[string]CDPCommandSchema{},
336380
commandParamsSchemas: map[string]map[string]any{},
337381
commandResultSchemas: map[string]map[string]any{},
@@ -375,6 +419,13 @@ func NewCDPTypes(customCommands []CustomCommand, customEvents []CustomEvent, cus
375419
panic(err)
376420
}
377421
}
422+
for _, customAliasObjects := range customAliasObjectGroups {
423+
for _, object := range customAliasObjects {
424+
if _, err := types.AddCustomAliasObject(object); err != nil {
425+
panic(err)
426+
}
427+
}
428+
}
378429
return types
379430
}
380431

@@ -389,11 +440,16 @@ func (types *CDPTypes) Update(config CDPTypesConfig) *CDPTypes {
389440
customEvents = append(customEvents, event)
390441
}
391442
customMiddlewares := append([]CustomMiddleware{}, types.CustomMiddlewares...)
443+
customAliasObjects := make([]CustomAliasObject, 0, len(types.CustomAliasObjects)+len(config.CustomAliasObjects))
444+
for _, object := range types.CustomAliasObjects {
445+
customAliasObjects = append(customAliasObjects, object)
446+
}
392447
types.mu.RUnlock()
393448
customCommands = append(customCommands, config.CustomCommands...)
394449
customEvents = append(customEvents, config.CustomEvents...)
395450
customMiddlewares = append(customMiddlewares, config.CustomMiddlewares...)
396-
return NewCDPTypes(customCommands, customEvents, customMiddlewares)
451+
customAliasObjects = append(customAliasObjects, config.CustomAliasObjects...)
452+
return NewCDPTypes(customCommands, customEvents, customMiddlewares, customAliasObjects)
397453
}
398454

399455
func (types *CDPTypes) ToJSON() map[string]any {
@@ -410,21 +466,24 @@ func (types *CDPTypes) ToJSON() map[string]any {
410466
}
411467
customMiddlewares = append(customMiddlewares, registration)
412468
}
469+
customAliasObjects := types.CustomAliasObjectWireRegistrations()
413470
types.mu.RLock()
414471
state := map[string]any{
415472
"custom_commands": len(types.CustomCommands),
416473
"custom_events": len(types.CustomEvents),
417474
"custom_middlewares": len(types.CustomMiddlewares),
475+
"custom_alias_objects": len(types.CustomAliasObjects),
418476
"command_params_schemas": len(types.commandParamsSchemas),
419477
"command_result_schemas": len(types.commandResultSchemas),
420478
"event_schemas": len(types.eventSchemas),
421479
}
422480
types.mu.RUnlock()
423481
return modtypes.ModCDPToJSON(types, modtypes.ModCDPJSONConfig{
424482
Config: map[string]any{
425-
"custom_commands": customCommands,
426-
"custom_events": types.CustomEventWireRegistrations(),
427-
"custom_middlewares": customMiddlewares,
483+
"custom_commands": customCommands,
484+
"custom_events": types.CustomEventWireRegistrations(),
485+
"custom_middlewares": customMiddlewares,
486+
"custom_alias_objects": customAliasObjects,
428487
},
429488
State: state,
430489
})
@@ -695,15 +754,21 @@ func (types *CDPTypes) AddCustomMiddleware(middleware CustomMiddleware) (string,
695754
return "", fmt.Errorf("phase must be request, response, or event")
696755
}
697756
middleware.Name = name
757+
types.mu.Lock()
758+
defer types.mu.Unlock()
698759
types.CustomMiddlewares = append(types.CustomMiddlewares, middleware)
699760
return name, nil
700761
}
701762

702763
func (types *CDPTypes) CustomMiddlewareWireRegistrations() []CustomMiddleware {
764+
types.mu.RLock()
765+
defer types.mu.RUnlock()
703766
return append([]CustomMiddleware{}, types.CustomMiddlewares...)
704767
}
705768

706769
func (types *CDPTypes) CustomMiddlewareRegistrations(phase string, name string) []CustomMiddleware {
770+
types.mu.RLock()
771+
defer types.mu.RUnlock()
707772
middlewares := []CustomMiddleware{}
708773
for _, middleware := range types.CustomMiddlewares {
709774
middlewareName := middleware.Name
@@ -717,6 +782,129 @@ func (types *CDPTypes) CustomMiddlewareRegistrations(phase string, name string)
717782
return middlewares
718783
}
719784

785+
func (types *CDPTypes) AddCustomAliasObject(object CustomAliasObject) (string, error) {
786+
name := strings.TrimSpace(object.Name)
787+
if name == "" {
788+
return "", fmt.Errorf("custom alias object name is required")
789+
}
790+
normalized := CustomAliasObject{
791+
Name: name,
792+
TypeName: strings.TrimSpace(object.TypeName),
793+
StickyFields: append([]string{}, object.StickyFields...),
794+
Methods: make([]AliasMethod, 0, len(object.Methods)),
795+
}
796+
if object.StickySchema != nil {
797+
normalized.StickySchema = cloneSchema(object.StickySchema)
798+
}
799+
for _, method := range object.Methods {
800+
methodName := strings.TrimSpace(method.Name)
801+
if methodName == "" {
802+
return "", fmt.Errorf("%s alias method name is required", name)
803+
}
804+
commandName := ""
805+
if strings.TrimSpace(method.Command) != "" {
806+
normalizedCommandName, err := normalizeModCDPName(method.Command)
807+
if err != nil {
808+
return "", fmt.Errorf("%s.%s command: %w", name, methodName, err)
809+
}
810+
commandName = normalizedCommandName
811+
}
812+
normalizedMethod := AliasMethod{
813+
Name: methodName,
814+
Command: commandName,
815+
SDKMethodName: strings.TrimSpace(method.SDKMethodName),
816+
StickyParam: strings.TrimSpace(method.StickyParam),
817+
StickyParams: append([]string{}, method.StickyParams...),
818+
StickyFields: append([]string{}, method.StickyFields...),
819+
}
820+
if method.ParamsSchema != nil {
821+
normalizedMethod.ParamsSchema = cloneSchema(method.ParamsSchema)
822+
}
823+
if method.ResultSchema != nil {
824+
normalizedMethod.ResultSchema = cloneSchema(method.ResultSchema)
825+
}
826+
if method.Return != nil {
827+
normalizedMethod.Return = &AliasReturn{
828+
Object: strings.TrimSpace(method.Return.Object),
829+
Unwrap: strings.TrimSpace(method.Return.Unwrap),
830+
Array: method.Return.Array,
831+
Nullable: method.Return.Nullable,
832+
}
833+
}
834+
normalized.Methods = append(normalized.Methods, normalizedMethod)
835+
}
836+
types.mu.Lock()
837+
defer types.mu.Unlock()
838+
for _, method := range normalized.Methods {
839+
if method.Command == "" {
840+
continue
841+
}
842+
if method.ParamsSchema != nil {
843+
if schema := cloneSchema(method.ParamsSchema); schema != nil {
844+
types.commandParamsSchemas[method.Command] = schema
845+
}
846+
}
847+
if method.ResultSchema != nil {
848+
if _, exists := types.commandResultSchemas[method.Command]; exists {
849+
continue
850+
}
851+
if schema := cloneSchema(method.ResultSchema); schema != nil {
852+
types.commandResultSchemas[method.Command] = schema
853+
}
854+
}
855+
}
856+
types.CustomAliasObjects[name] = normalized
857+
return name, nil
858+
}
859+
860+
func (types *CDPTypes) CustomAliasObjectWireRegistrations() []CustomAliasObject {
861+
types.mu.RLock()
862+
objects := make([]CustomAliasObject, 0, len(types.CustomAliasObjects))
863+
for _, object := range types.CustomAliasObjects {
864+
objects = append(objects, object)
865+
}
866+
types.mu.RUnlock()
867+
registrations := make([]CustomAliasObject, 0, len(objects))
868+
for _, object := range objects {
869+
registration := CustomAliasObject{
870+
Name: object.Name,
871+
TypeName: object.TypeName,
872+
StickyFields: append([]string{}, object.StickyFields...),
873+
Methods: make([]AliasMethod, 0, len(object.Methods)),
874+
}
875+
if object.StickySchema != nil {
876+
registration.StickySchema = cloneSchema(object.StickySchema)
877+
}
878+
for _, method := range object.Methods {
879+
methodRegistration := AliasMethod{
880+
Name: method.Name,
881+
Command: method.Command,
882+
SDKMethodName: method.SDKMethodName,
883+
StickyParam: method.StickyParam,
884+
StickyParams: append([]string{}, method.StickyParams...),
885+
StickyFields: append([]string{}, method.StickyFields...),
886+
}
887+
if method.ParamsSchema != nil {
888+
methodRegistration.ParamsSchema = cloneSchema(method.ParamsSchema)
889+
}
890+
if method.ResultSchema != nil {
891+
methodRegistration.ResultSchema = cloneSchema(method.ResultSchema)
892+
}
893+
if method.Return != nil {
894+
methodRegistration.Return = &AliasReturn{
895+
Object: method.Return.Object,
896+
Unwrap: method.Return.Unwrap,
897+
Array: method.Return.Array,
898+
Nullable: method.Return.Nullable,
899+
}
900+
}
901+
registration.Methods = append(registration.Methods, methodRegistration)
902+
}
903+
registrations = append(registrations, registration)
904+
}
905+
return registrations
906+
}
907+
720908
func (types *CDPTypes) ServiceWorkerCommandStep(method string, params map[string]any, cdpSessionID string, executionContextID int) (modtypes.TranslatedStep, error) {
721909
if params == nil {
722910
params = map[string]any{}

go/modcdp/client/ModCDPClient.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ type AutoSessionRouter = router.AutoSessionRouter
8888
type CustomCommand = types.ModCDPAddCustomCommandParams
8989
type CustomEvent = types.ModCDPAddCustomEventObjectParams
9090
type CustomMiddleware = types.ModCDPAddMiddlewareParams
91+
type CustomAliasObject = types.ModCDPAliasObject
92+
type AliasMethod = types.ModCDPAliasMethod
93+
type AliasReturn = types.ModCDPAliasReturn
9194

9295
var NewLocalBrowserLauncher = launcher.NewLocalBrowserLauncher
9396
var NewRemoteBrowserLauncher = launcher.NewRemoteBrowserLauncher
@@ -163,9 +166,10 @@ func freePort() (int, error) {
163166
}
164167

165168
type CDPTypesConfig struct {
166-
CustomCommands []CustomCommand `json:"custom_commands,omitempty"`
167-
CustomEvents []CustomEvent `json:"custom_events,omitempty"`
168-
CustomMiddlewares []CustomMiddleware `json:"custom_middlewares,omitempty"`
169+
CustomCommands []CustomCommand `json:"custom_commands,omitempty"`
170+
CustomEvents []CustomEvent `json:"custom_events,omitempty"`
171+
CustomMiddlewares []CustomMiddleware `json:"custom_middlewares,omitempty"`
172+
CustomAliasObjects []CustomAliasObject `json:"custom_alias_objects,omitempty"`
169173
}
170174

171175
type ServerConfig struct {
@@ -177,6 +181,7 @@ type ServerConfig struct {
177181
CustomCommands []CustomCommand `json:"custom_commands,omitempty"`
178182
CustomEvents []CustomEvent `json:"custom_events,omitempty"`
179183
CustomMiddlewares []CustomMiddleware `json:"custom_middlewares,omitempty"`
184+
CustomAliasObjects []CustomAliasObject `json:"custom_alias_objects,omitempty"`
180185
disabled bool
181186
}
182187

@@ -406,7 +411,7 @@ func New(config Config) *ModCDPClient {
406411
upstream := NewWSUpstreamTransport(config.Upstream)
407412
client := &ModCDPClient{
408413
Config: config,
409-
Types: NewCDPTypes(typesConfig.CustomCommands, typesConfig.CustomEvents, typesConfig.CustomMiddlewares),
414+
Types: NewCDPTypes(typesConfig.CustomCommands, typesConfig.CustomEvents, typesConfig.CustomMiddlewares, typesConfig.CustomAliasObjects),
410415
Upstream: upstream,
411416
handlers: map[string][]handlerEntry{},
412417
}

0 commit comments

Comments
 (0)