Skip to content

Commit e00f167

Browse files
committed
v0.5.10
1 parent 424b109 commit e00f167

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1609
-789
lines changed

adapter/database/settings.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ import (
1313
const (
1414
settingsTable = "settings"
1515
settingsTableFields = `window_width, window_height, window_x, window_y, single_instance, connect_timeout,
16-
request_timeout, k8s_request_timeout, non_blocking_connection, sort_methods_by_name, max_loop_depth`
16+
request_timeout, k8s_request_timeout, non_blocking_connection, sort_methods_by_name, max_loop_depth,
17+
emit_defaults, check_updates`
1718
)
1819

19-
// SettingsRepository object capable of interacting with SettingsRepository
20+
// SettingsRepository object capable of interacting with SettingsRepository.
2021
type SettingsRepository struct {
2122
db *database.Database
2223
ctx context.Context
2324
}
2425

25-
// NewSettingsRepository creates a new SettingsRepository
26+
// NewSettingsRepository creates a new SettingsRepository.
2627
func NewSettingsRepository(ctx context.Context, db *database.Database) *SettingsRepository {
2728
return &SettingsRepository{
2829
db: db,
@@ -42,6 +43,8 @@ type settingsDTO struct {
4243
NonBlockingConnection bool `db:"non_blocking_connection"`
4344
SortMethodsByName bool `db:"sort_methods_by_name"`
4445
MaxLoopDepth int `db:"max_loop_depth"`
46+
EmitDefaults bool `db:"emit_defaults"`
47+
CheckUpdates bool `db:"check_updates"`
4548
}
4649

4750
func (dto *settingsDTO) entity() *entity.Settings {
@@ -57,10 +60,12 @@ func (dto *settingsDTO) entity() *entity.Settings {
5760
NonBlockingConnection: &dto.NonBlockingConnection,
5861
SortMethodsByName: &dto.SortMethodsByName,
5962
MaxLoopDepth: &dto.MaxLoopDepth,
63+
EmitDefaults: &dto.EmitDefaults,
64+
CheckUpdates: &dto.CheckUpdates,
6065
}
6166
}
6267

63-
// Get returns Settings
68+
// Get returns Settings.
6469
func (repo *SettingsRepository) Get() (*entity.Settings, error) {
6570
dto := &settingsDTO{}
6671

@@ -72,11 +77,11 @@ func (repo *SettingsRepository) Get() (*entity.Settings, error) {
7277
return dto.entity(), nil
7378
}
7479

75-
// Update updates Settings
80+
// Update updates Settings.
7681
func (repo *SettingsRepository) Update(in *entity.Settings) (*entity.Settings, error) {
7782
dto := &settingsDTO{}
78-
attrs := make([]string, 0, 10)
79-
mapper := make(map[string]interface{}, 11)
83+
attrs := make([]string, 0, 13)
84+
mapper := make(map[string]interface{}, 13)
8085

8186
if in.WindowWidth > 0 {
8287
attrs = append(attrs, "window_width = :window_width")
@@ -122,6 +127,14 @@ func (repo *SettingsRepository) Update(in *entity.Settings) (*entity.Settings, e
122127
attrs = append(attrs, "max_loop_depth = :max_loop_depth")
123128
mapper["max_loop_depth"] = in.MaxLoopDepth
124129
}
130+
if in.EmitDefaults != nil {
131+
attrs = append(attrs, "emit_defaults = :emit_defaults")
132+
mapper["emit_defaults"] = in.EmitDefaults
133+
}
134+
if in.CheckUpdates != nil {
135+
attrs = append(attrs, "check_updates = :check_updates")
136+
mapper["check_updates"] = in.CheckUpdates
137+
}
125138
if len(attrs) == 0 {
126139
return repo.Get()
127140
}

adapter/database/workspace.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ const (
2323
workspaceTableFields = "id, parent_id, has_child, type, title, data, sort, expanded"
2424
)
2525

26-
// WorkspaceRepository object capable of interacting with WorkspaceRepository
26+
// WorkspaceRepository object capable of interacting with WorkspaceRepository.
2727
type WorkspaceRepository struct {
2828
db *database.Database
2929
ctx context.Context
3030
log *logger.Zerolog
3131
}
3232

33-
// NewWorkspaceRepository creates a new WorkspaceRepository
33+
// NewWorkspaceRepository creates a new WorkspaceRepository.
3434
func NewWorkspaceRepository(ctx context.Context, db *database.Database, log *logger.Zerolog) *WorkspaceRepository {
3535
return &WorkspaceRepository{
3636
db: db,
@@ -101,7 +101,7 @@ func (dto *workspaceDTO) entity() (*entity.Workspace, error) {
101101
return out, nil
102102
}
103103

104-
// GetByID returns workspace item by id
104+
// GetByID returns workspace item by id.
105105
func (repo *WorkspaceRepository) GetByID(id int64) (*entity.Workspace, error) {
106106
dto := &workspaceDTO{ID: id}
107107

@@ -126,7 +126,7 @@ func (repo *WorkspaceRepository) GetByID(id int64) (*entity.Workspace, error) {
126126
return nil, entity.ErrWorkspaceNotExists
127127
}
128128

129-
// GetByParentID returns workspace item by parent id
129+
// GetByParentID returns workspace item by parent id.
130130
func (repo *WorkspaceRepository) GetByParentID(parentID int64, tx *sqlx.Tx) ([]*entity.Workspace, error) {
131131
dto := &workspaceDTO{ParentID: types.Int64ToSQL(parentID)}
132132
res := make([]*entity.Workspace, 0, 10)
@@ -165,7 +165,7 @@ func (repo *WorkspaceRepository) GetByParentID(parentID int64, tx *sqlx.Tx) ([]*
165165
return res, nil
166166
}
167167

168-
// Get returns workspace
168+
// Get returns workspace.
169169
func (repo *WorkspaceRepository) Get() ([]*entity.Workspace, error) {
170170
var dto []*workspaceDTO
171171

@@ -185,7 +185,7 @@ func (repo *WorkspaceRepository) Get() ([]*entity.Workspace, error) {
185185
return resp, nil
186186
}
187187

188-
// Create creates new workspace item
188+
// Create creates new workspace item.
189189
func (repo *WorkspaceRepository) Create(in *entity.Workspace) (*entity.Workspace, error) {
190190
dto, err := newWorkspaceDTO(in)
191191
if err != nil {
@@ -220,7 +220,7 @@ func (repo *WorkspaceRepository) Create(in *entity.Workspace) (*entity.Workspace
220220
return dto.entity()
221221
}
222222

223-
// Update updates workspace item
223+
// Update updates workspace item.
224224
func (repo *WorkspaceRepository) Update(in *entity.Workspace) (*entity.Workspace, error) {
225225
dto := &workspaceDTO{}
226226
attrs := make([]string, 0, 6)
@@ -248,7 +248,7 @@ func (repo *WorkspaceRepository) Update(in *entity.Workspace) (*entity.Workspace
248248
}
249249
if in.Sort != nil {
250250
attrs = append(attrs, "sort = :sort")
251-
mapper["sort"] = *in.Sort
251+
mapper["sort"] = in.Sort
252252
}
253253
if in.Expanded != nil {
254254
attrs = append(attrs, "expanded = :expanded")
@@ -290,7 +290,7 @@ func (repo *WorkspaceRepository) Update(in *entity.Workspace) (*entity.Workspace
290290
return dto.entity()
291291
}
292292

293-
// Delete deletes workspace item
293+
// Delete deletes workspace item.
294294
func (repo *WorkspaceRepository) Delete(id int64) error {
295295
workspace, err := repo.GetByID(id)
296296
if err != nil {

adapter/grpc/auth.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package grpc
33
import (
44
"context"
55
"encoding/base64"
6+
"errors"
67
"fmt"
78

89
"github.com/golang-jwt/jwt/v5"
@@ -18,6 +19,10 @@ type basicAuth struct {
1819
password string
1920
}
2021

22+
type oauthToken struct {
23+
token *oauth2.Token
24+
}
25+
2126
var symmetricAlgorithms = map[string]struct{}{
2227
"HS256": {},
2328
"HS384": {},
@@ -57,7 +62,7 @@ func (c *Client) authBearer(auth *entity.Auth) (grpc.DialOption, error) {
5762
token.TokenType = auth.HeaderPrefix
5863

5964
return grpc.WithPerRPCCredentials(
60-
oauth.NewOauthAccess(token),
65+
oauth.TokenSource{TokenSource: oauthToken{token: token}},
6166
), nil
6267
}
6368

@@ -118,3 +123,10 @@ func (b basicAuth) GetRequestMetadata(ctx context.Context, _ ...string) (map[str
118123
func (b basicAuth) RequireTransportSecurity() bool {
119124
return true
120125
}
126+
127+
func (t oauthToken) Token() (*oauth2.Token, error) {
128+
if t.token == nil {
129+
return nil, errors.New("nil token")
130+
}
131+
return t.token, nil
132+
}

adapter/grpc/client.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"context"
66
"crypto/tls"
77
"crypto/x509"
8-
"fmt"
8+
"errors"
99
"sync"
1010
"time"
1111

@@ -25,7 +25,7 @@ const (
2525
responseChanCapacity = 10
2626
)
2727

28-
// Client object capable of interacting with Client
28+
// Client object capable of interacting with Client.
2929
type Client struct {
3030
ctx context.Context
3131
cfg *entity.Settings
@@ -45,7 +45,7 @@ type Client struct {
4545
importPath []string
4646
}
4747

48-
// New creates a new Client
48+
// New creates a new Client.
4949
func New(ctx context.Context, log *logger.Zerolog) *Client {
5050
return &Client{
5151
ctx: ctx,
@@ -55,12 +55,12 @@ func New(ctx context.Context, log *logger.Zerolog) *Client {
5555
}
5656
}
5757

58-
// SetSettings sets application settings
58+
// SetSettings sets application settings.
5959
func (c *Client) SetSettings(cfg *entity.Settings) {
6060
c.cfg = cfg
6161
}
6262

63-
// Connect connecting to gRPC server
63+
// Connect connecting to gRPC server.
6464
func (c *Client) Connect(addr string, auth *entity.Auth, opts ...ClientOpt) error {
6565
if defaultOptions != nil {
6666
c.opts = *defaultOptions
@@ -122,9 +122,10 @@ func (c *Client) getDialOptions() ([]grpc.DialOption, error) {
122122
func (c *Client) loadTLSCredentials() (credentials.TransportCredentials, error) {
123123
pool := x509.NewCertPool()
124124
if !pool.AppendCertsFromPEM([]byte(c.opts.rootCertificate)) {
125-
return nil, fmt.Errorf("failed to add server CA's certificate")
125+
return nil, errors.New("failed to add server CA's certificate")
126126
}
127127

128+
// nolint:gosec
128129
cfg := &tls.Config{
129130
RootCAs: pool,
130131
InsecureSkipVerify: c.opts.insecureSkipVerify,
@@ -141,7 +142,7 @@ func (c *Client) loadTLSCredentials() (credentials.TransportCredentials, error)
141142
return credentials.NewTLS(cfg), nil
142143
}
143144

144-
// Close closes connection to gRPC server
145+
// Close closes connection to gRPC server.
145146
func (c *Client) Close() {
146147
c.connectionMux.Lock()
147148
defer c.connectionMux.Unlock()

adapter/grpc/misc.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package grpc
33

44
import (
55
"sort"
6+
"strings"
67

78
"github.com/forest33/warthog/business/entity"
89
)
910

1011
func (c *Client) sortServicesByName(services []*entity.Service) {
1112
sort.Slice(services, func(i, j int) bool {
12-
if services[j].Name == entity.ReflectionServiceFQN {
13+
if strings.HasPrefix(services[j].Name, entity.ReflectionServicePrefix) {
1314
return true
1415
}
1516
return services[i].Name < services[j].Name

adapter/grpc/options.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Package grpc provides basic gRPC functions.
22
package grpc
33

4-
// ClientOptions represents Client options
4+
// ClientOptions represents Client options.
55
type ClientOptions struct {
66
noTLS bool
77
insecureSkipVerify bool
@@ -10,7 +10,7 @@ type ClientOptions struct {
1010
clientKey string
1111
}
1212

13-
// ClientOpt represents Client option
13+
// ClientOpt represents Client option.
1414
type ClientOpt func(options *ClientOptions)
1515

1616
var defaultOptions = &ClientOptions{
@@ -21,35 +21,35 @@ var defaultOptions = &ClientOptions{
2121
clientKey: "",
2222
}
2323

24-
// WithNoTLS returns ClientOpt which disables transport security
24+
// WithNoTLS returns ClientOpt which disables transport security.
2525
func WithNoTLS() ClientOpt {
2626
return func(options *ClientOptions) {
2727
options.noTLS = true
2828
}
2929
}
3030

31-
// WithInsecure returns ClientOpt which disables server certificate chain verification and hostname
31+
// WithInsecure returns ClientOpt which disables server certificate chain verification and hostname.
3232
func WithInsecure() ClientOpt {
3333
return func(options *ClientOptions) {
3434
options.insecureSkipVerify = true
3535
}
3636
}
3737

38-
// WithRootCertificate returns ClientOpt which sets server CA certificate
38+
// WithRootCertificate returns ClientOpt which sets server CA certificate.
3939
func WithRootCertificate(cert string) ClientOpt {
4040
return func(options *ClientOptions) {
4141
options.rootCertificate = cert
4242
}
4343
}
4444

45-
// WithClientCertificate returns ClientOpt which sets client certificate
45+
// WithClientCertificate returns ClientOpt which sets client certificate.
4646
func WithClientCertificate(cert string) ClientOpt {
4747
return func(options *ClientOptions) {
4848
options.clientCertificate = cert
4949
}
5050
}
5151

52-
// WithClientKey returns ClientOpt which sets client certificate private key
52+
// WithClientKey returns ClientOpt which sets client certificate private key.
5353
func WithClientKey(key string) ClientOpt {
5454
return func(options *ClientOptions) {
5555
options.clientKey = key

adapter/grpc/proto.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package grpc
33

44
import (
55
"context"
6+
"errors"
67
"fmt"
78
"strings"
89
"time"
@@ -15,20 +16,20 @@ import (
1516
"github.com/forest33/warthog/business/entity"
1617
)
1718

18-
// AddProtobuf adds protobuf files
19+
// AddProtobuf adds protobuf files.
1920
func (c *Client) AddProtobuf(path ...string) {
2021
c.protoPath = path
2122
}
2223

23-
// AddImport adds import paths
24+
// AddImport adds import paths.
2425
func (c *Client) AddImport(path ...string) {
2526
c.importPath = path
2627
}
2728

28-
// LoadFromProtobuf loads services from protobuf
29+
// LoadFromProtobuf loads services from protobuf.
2930
func (c *Client) LoadFromProtobuf() ([]*entity.Service, []*entity.ProtobufError, *entity.ProtobufError) {
3031
if len(c.protoPath) == 0 {
31-
return nil, nil, &entity.ProtobufError{Err: fmt.Errorf("empty path to protobuf's")}
32+
return nil, nil, &entity.ProtobufError{Err: errors.New("empty path to protobuf's")}
3233
}
3334

3435
var (
@@ -70,7 +71,7 @@ func (c *Client) LoadFromProtobuf() ([]*entity.Service, []*entity.ProtobufError,
7071
return nil, nil, protoErr
7172
}
7273
if len(fd) != 1 {
73-
return nil, nil, &entity.ProtobufError{Err: fmt.Errorf("wrong parse result")}
74+
return nil, nil, &entity.ProtobufError{Err: errors.New("wrong parse result")}
7475
}
7576

7677
for _, sd := range fd[0].GetServices() {
@@ -87,7 +88,7 @@ func (c *Client) LoadFromProtobuf() ([]*entity.Service, []*entity.ProtobufError,
8788
return services, protoWarn, nil
8889
}
8990

90-
// LoadFromReflection loads services using reflection
91+
// LoadFromReflection loads services using reflection.
9192
func (c *Client) LoadFromReflection() ([]*entity.Service, error) {
9293
ctx, cancel := context.WithTimeout(c.ctx, time.Second*time.Duration(*c.cfg.ConnectTimeout))
9394
defer cancel()
@@ -258,13 +259,14 @@ func getMapFieldTypeName(f *desc.FieldDescriptor) (string, string, string) {
258259
var valueTypeName string
259260
keyType := f.GetMapKeyType().GetType().String()
260261
valueType := f.GetMapValueType().GetType().String()
261-
if valueType == entity.ProtoTypeMessage {
262+
switch valueType {
263+
case entity.ProtoTypeMessage:
262264
if mt := f.GetMapValueType().GetMessageType(); mt != nil {
263265
valueTypeName = mt.GetName()
264266
}
265-
} else if valueType == entity.ProtoTypeEnum {
267+
case entity.ProtoTypeEnum:
266268
valueTypeName = f.GetMapValueType().GetEnumType().GetName()
267-
} else {
269+
default:
268270
valueTypeName = valueType
269271
}
270272
return keyType, valueType, valueTypeName

0 commit comments

Comments
 (0)