Skip to content

Commit 459ec84

Browse files
authored
types: Add UnimplementedStub to have forward compatible implementations (#524)
* types: Implement UnimplementedStub to have forward compatible implementations Signed-off-by: Xuanwo <github@xuanwo.io> * Add mustEmbedUnimplementedXXX function Signed-off-by: Xuanwo <github@xuanwo.io>
1 parent 93fb16e commit 459ec84

File tree

4 files changed

+411
-3
lines changed

4 files changed

+411
-3
lines changed

cmd/definitions/bindata.go

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

cmd/definitions/tmpl/operation.tmpl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,33 @@ type {{ $i.DisplayName }} interface {
2020
{{ $op.Name | toPascal }}WithContext(ctx context.Context,{{ $op.FormatParams }}) ({{ $op.FormatResultsWithPackageName "storage" }})
2121
{{ end }}
2222
{{ end }}
23+
24+
mustEmbedUnimplemented{{ $i.DisplayName }}()
25+
}
26+
27+
// Unimplemented{{ $i.DisplayName }} must be embedded to have forward compatible implementations.
28+
type Unimplemented{{ $i.DisplayName }} struct {}
29+
30+
func (s Unimplemented{{ $i.DisplayName }}) mustEmbedUnimplemented{{ $i.DisplayName }}() {}
31+
32+
func (s Unimplemented{{ $i.DisplayName }}) String() string {
33+
return "Unimplemented{{ $i.DisplayName }}"
34+
}
35+
36+
{{ range $_, $op := $i.Ops }}
37+
func (s Unimplemented{{ $i.DisplayName }}) {{ $op.Name | toPascal }}({{ $op.FormatParams }}) ({{ $op.FormatResultsWithPackageName "storage" }}) {
38+
{{- if not $op.Local }}
39+
err = NewOperationNotImplementedError("{{ $op.Name }}")
40+
{{- end }}
41+
return
42+
}
43+
{{- if not $op.Local }}
44+
func (s Unimplemented{{ $i.DisplayName }}) {{ $op.Name | toPascal }}WithContext(ctx context.Context,{{ $op.FormatParams }}) ({{ $op.FormatResultsWithPackageName "storage" }}) {
45+
err = NewOperationNotImplementedError("{{ $op.Name }}")
46+
return
2347
}
48+
{{ end }}
49+
{{ end }}
2450
{{- end }}
2551

2652
type PairPolicy struct {

types/error.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package types
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var (
9+
// ErrNotImplemented will be returned while this operation is not
10+
// implemented by services.
11+
ErrNotImplemented = errors.New("not implemented")
12+
)
13+
14+
// OperationError is the error for operation related errors.
15+
type OperationError struct {
16+
op string
17+
err error
18+
}
19+
20+
func (oe OperationError) Error() string {
21+
return fmt.Sprintf("operation %s: %v", oe.op, oe.err)
22+
}
23+
24+
func (oe OperationError) Unwrap() error {
25+
return oe.err
26+
}
27+
28+
// NewOperationNotImplementedError will create a new NotImplemented error.
29+
func NewOperationNotImplementedError(op string) error {
30+
return OperationError{
31+
op: op,
32+
err: ErrNotImplemented,
33+
}
34+
}

0 commit comments

Comments
 (0)