Skip to content

Commit 08ecdf9

Browse files
committed
Add labels
1 parent c3a99e8 commit 08ecdf9

File tree

15 files changed

+258
-174
lines changed

15 files changed

+258
-174
lines changed

pkg/capabilities/v2/actions/http/server/client_server_gen.go

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/capabilities/v2/chain-capabilities/evm/server/client_server_gen.go

Lines changed: 17 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/capabilities/v2/consensus/server/consensus_server_gen.go

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/capabilities/v2/protoc/pkg/generate_server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
_ "embed"
55
"errors"
66
"fmt"
7+
"text/template"
78

89
"google.golang.org/protobuf/compiler/protogen"
910
)
@@ -35,6 +36,8 @@ var serverTemplates = map[ServerLanguage]TemplateGenerator{
3536
Name: "go_server",
3637
Template: goServerTemplate,
3738
FileNameTemplate: "server/{{.}}_server_gen.go",
39+
LabelMapper: PbLabelToGoLabels,
40+
ExtraFns: template.FuncMap{"AppendLabels": AppendGoLabelsToVersion},
3841
},
3942
}
4043

pkg/capabilities/v2/protoc/pkg/template_generator.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type TemplateGenerator struct {
2020
Template string
2121
FileNameTemplate string
2222
Partials map[string]string
23+
LabelMapper func(lbls map[string]*pb.Label) ([]Label, error)
2324
ExtraFns template.FuncMap
2425
}
2526

@@ -198,7 +199,13 @@ func (t *TemplateGenerator) runTemplate(name, tmplText string, args any, partial
198199
return line
199200
}
200201
},
201-
202+
"Labels": func(s *protogen.Service) ([]Label, error) {
203+
md, err := getCapabilityMetadata(s)
204+
if err != nil {
205+
return nil, err
206+
}
207+
return t.LabelMapper(md.Labels)
208+
},
202209
"ConfigType": func(s *protogen.Service) (string, error) {
203210
md, err := getCapabilityMetadata(s)
204211
if err != nil {

pkg/capabilities/v2/protoc/pkg/templates/server.go.tmpl

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,27 @@ import (
2626
// Avoid unused imports if there is configuration type
2727
var _ = emptypb.Empty{}
2828

29-
{{ range .Services}}
29+
{{- range .Services}}
3030
{{ $hasTriggers := false }}
3131
{{ $hasActions := false }}
32-
{{ $service := . -}}
33-
{{ if .Comments.Leading.String }}//{{.GoName}}Capability {{CleanComments .Comments.Leading.String}} {{ end }}
34-
type {{.GoName}}Capability interface { {{- if .Comments.Trailing.String }}//{{.GoName}}Capability {{CleanComments .Comments.Trailing.String}} {{ end }}
32+
{{ $service := . }}
33+
{{ $fullCapabilityId := printf "\"%s\"%s" (CapabilityId $service) (AppendLabels (Labels $service)) }}
34+
type {{.GoName}}Capability interface {
3535
{{- range .Methods}}
3636
{{- if isTrigger . }}
3737
{{ $hasTriggers = true }}
38-
{{- if .Comments.Leading.String }}//Register{{.GoName}} registration for {{CleanComments .Comments.Leading.String}} {{ end }}
39-
Register{{.GoName}}(ctx context.Context, triggerID string, metadata capabilities.RequestMetadata, input *{{ImportAlias .Input.GoIdent.GoImportPath}}.{{.Input.GoIdent.GoName}}) (<- chan capabilities.TriggerAndId[*{{ImportAlias .Output.GoIdent.GoImportPath}}.{{.Output.GoIdent.GoName}}], error) {{- if .Comments.Trailing.String }}// {{CleanComments .Comments.Trailing.String}} {{ end }}
40-
{{ if .Comments.Leading.String }}//Unregister{{.GoName}} unregistration for {{CleanComments .Comments.Leading.String}} {{ end }}
41-
Unregister{{.GoName}}(ctx context.Context, triggerID string, metadata capabilities.RequestMetadata, input *{{ImportAlias .Input.GoIdent.GoImportPath}}.{{.Input.GoIdent.GoName}}) error {{- if .Comments.Trailing.String }}// {{CleanComments .Comments.Trailing.String}} {{ end }}
38+
Register{{.GoName}}(ctx context.Context, triggerID string, metadata capabilities.RequestMetadata, input *{{ImportAlias .Input.GoIdent.GoImportPath}}.{{.Input.GoIdent.GoName}}) (<- chan capabilities.TriggerAndId[*{{ImportAlias .Output.GoIdent.GoImportPath}}.{{.Output.GoIdent.GoName}}], error)
39+
Unregister{{.GoName}}(ctx context.Context, triggerID string, metadata capabilities.RequestMetadata, input *{{ImportAlias .Input.GoIdent.GoImportPath}}.{{.Input.GoIdent.GoName}}) error
4240
{{- else }}
4341
{{ $hasActions = true }}
44-
{{- if .Comments.Leading.String }}//{{.GoName}} {{CleanComments .Comments.Leading.String}} {{ end }}
45-
{{.GoName}}(ctx context.Context, metadata capabilities.RequestMetadata, input *{{ImportAlias .Input.GoIdent.GoImportPath}}.{{.Input.GoIdent.GoName}} {{if ne "emptypb.Empty" (ConfigType $service)}}, {{(ConfigType $service)}}{{ end }}) (*{{ImportAlias .Output.GoIdent.GoImportPath}}.{{.Output.GoIdent.GoName}}, error) {{- if .Comments.Trailing.String }}// {{CleanComments .Comments.Trailing.String}} {{ end }}
46-
{{ end }}
42+
{{.GoName}}(ctx context.Context, metadata capabilities.RequestMetadata, input *{{ImportAlias .Input.GoIdent.GoImportPath}}.{{.Input.GoIdent.GoName}} {{if ne "emptypb.Empty" (ConfigType $service)}}, {{(ConfigType $service)}}{{ end }}) (*{{ImportAlias .Output.GoIdent.GoImportPath}}.{{.Output.GoIdent.GoName}}, error)
43+
{{- end }}
4744
{{- end }}
4845

46+
{{ range Labels . }}
47+
{{.Name}}() {{.Type}}
48+
{{ end }}
49+
4950
Start(ctx context.Context) error
5051
Close() error
5152
HealthReport() map[string]error
@@ -62,50 +63,50 @@ func New{{.GoName}}Server(capability {{.GoName}}Capability) *{{.GoName}}Server {
6263
stopCh: stopCh,
6364
}
6465
}
65-
{{ if .Comments.Leading.String }}//{{.GoName}}Server {{CleanComments .Comments.Leading.String}} {{ end }}
66-
type {{.GoName}}Server struct { {{- if .Comments.Trailing.String }}// {{CleanComments .Comments.Trailing.String}} {{ end }}
66+
67+
type {{.GoName}}Server struct {
6768
{{.GoName|LowerFirst}}Capability
6869
capabilityRegistry core.CapabilitiesRegistry
6970
stopCh chan struct{}
7071
}
7172

7273

73-
func (cs *{{.GoName}}Server) Initialise(ctx context.Context, config string, telemetryService core.TelemetryService, store core.KeyValueStore, capabilityRegistry core.CapabilitiesRegistry, errorLog core.ErrorLog, pipelineRunner core.PipelineRunnerService, relayerSet core.RelayerSet, oracleFactory core.OracleFactory, gatewayConnector core.GatewayConnector, p2pKeystore core.Keystore) error {
74-
if err := cs.{{.GoName}}Capability.Initialise(ctx, config, telemetryService, store, errorLog, pipelineRunner, relayerSet, oracleFactory, gatewayConnector, p2pKeystore); err != nil {
74+
func (c *{{.GoName}}Server) Initialise(ctx context.Context, config string, telemetryService core.TelemetryService, store core.KeyValueStore, capabilityRegistry core.CapabilitiesRegistry, errorLog core.ErrorLog, pipelineRunner core.PipelineRunnerService, relayerSet core.RelayerSet, oracleFactory core.OracleFactory, gatewayConnector core.GatewayConnector, p2pKeystore core.Keystore) error {
75+
if err := c.{{.GoName}}Capability.Initialise(ctx, config, telemetryService, store, errorLog, pipelineRunner, relayerSet, oracleFactory, gatewayConnector, p2pKeystore); err != nil {
7576
return fmt.Errorf("error when initializing capability: %w", err)
7677
}
7778

78-
cs.capabilityRegistry = capabilityRegistry
79+
c.capabilityRegistry = capabilityRegistry
7980

8081
if err := capabilityRegistry.Add(ctx, &{{.GoName|LowerFirst}}Capability{
81-
{{.GoName}}Capability: cs.{{.GoName}}Capability,
82+
{{.GoName}}Capability: c.{{.GoName}}Capability,
8283
}); err != nil {
8384
return fmt.Errorf("error when adding kv store action to the registry: %w", err)
8485
}
8586

8687
return nil
8788
}
8889

89-
func (cs *{{.GoName}}Server) Close() error{
90+
func (c *{{.GoName}}Server) Close() error{
9091
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
9192
defer cancel()
9293

93-
if cs.capabilityRegistry != nil {
94-
if err := cs.capabilityRegistry.Remove(ctx, "{{(CapabilityId $service)}}"); err != nil {
94+
if c.capabilityRegistry != nil {
95+
if err := c.capabilityRegistry.Remove(ctx, {{$fullCapabilityId}}); err != nil {
9596
return err
9697
}
9798
}
9899

99-
if cs.stopCh != nil {
100-
close(cs.stopCh)
100+
if c.stopCh != nil {
101+
close(c.stopCh)
101102
}
102103

103-
return cs.{{.GoName|LowerFirst}}Capability.Close()
104+
return c.{{.GoName|LowerFirst}}Capability.Close()
104105
}
105106

106107

107-
func (cs *{{.GoName}}Server) Infos(ctx context.Context) ([]capabilities.CapabilityInfo, error) {
108-
info, err := cs.{{.GoName|LowerFirst}}Capability.Info(ctx)
108+
func (c *{{.GoName}}Server) Infos(ctx context.Context) ([]capabilities.CapabilityInfo, error) {
109+
info, err := c.{{.GoName|LowerFirst}}Capability.Info(ctx)
109110
if err != nil {
110111
return nil, err
111112
}
@@ -119,7 +120,7 @@ type {{.GoName|LowerFirst}}Capability struct {
119120

120121
func (c *{{.GoName|LowerFirst}}Capability) Info(ctx context.Context) (capabilities.CapabilityInfo, error) {
121122
// Maybe we do need to split it out, even if the user doesn't see it
122-
return capabilities.NewCapabilityInfo("{{(CapabilityId $service)}}", capabilities.CapabilityTypeCombined, c.{{.GoName}}Capability.Description())
123+
return capabilities.NewCapabilityInfo({{$fullCapabilityId}}, capabilities.CapabilityTypeCombined, c.{{.GoName}}Capability.Description())
123124
}
124125

125126

@@ -134,7 +135,7 @@ func (c *{{.GoName|LowerFirst}}Capability) RegisterTrigger(ctx context.Context,
134135
{{- if (isTrigger .) }}
135136
case {{- if (MapToUntypedAPI .) }} "" {{- else}} "{{.GoName}}" {{- end }}:
136137
input := &{{ImportAlias .Input.GoIdent.GoImportPath}}.{{.Input.GoIdent.GoName}}{}
137-
return capabilities.RegisterTrigger(ctx, c.stopCh, "{{(CapabilityId $service)}}", request, input, c.{{$service.GoName}}Capability.Register{{.GoName}})
138+
return capabilities.RegisterTrigger(ctx, c.stopCh, {{$fullCapabilityId}}, request, input, c.{{$service.GoName}}Capability.Register{{.GoName}})
138139
{{- end }}
139140
{{- end }}
140141
default:

0 commit comments

Comments
 (0)