implement Save

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-05-09 18:47:50 +03:00
parent 1658fc9a93
commit b18033c417
6 changed files with 23 additions and 11 deletions

View File

@ -1,6 +1,6 @@
// Code generated by protoc-gen-micro
// source: service.proto
package service
package servicepb
import (
context "context"

View File

@ -1,6 +1,6 @@
// Code generated by protoc-gen-micro
// source: service.proto
package service
package servicepb
import (
context "context"
@ -58,8 +58,9 @@ func RegisterConfigServer(s server.Server, sh ConfigServer, opts ...server.Handl
config
}
h := &configServer{sh}
var nopts []server.HandlerOption
for _, endpoint := range NewConfigEndpoints() {
opts = append(opts, api.WithEndpoint(endpoint))
nopts = append(nopts, api.WithEndpoint(endpoint))
}
return s.Handle(s.NewHandler(&Config{h}, opts...))
return s.Handle(s.NewHandler(&Config{h}, append(nopts, opts...)...))
}

View File

@ -1,10 +1,10 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.6.1
// protoc v3.15.6
// source: service.proto
package service
package servicepb
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
@ -229,11 +229,11 @@ var file_service_proto_rawDesc = []byte{
0x61, 0x76, 0x65, 0x12, 0x14, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x53, 0x61,
0x76, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x73, 0x65, 0x72, 0x76,
0x69, 0x63, 0x65, 0x2e, 0x53, 0x61, 0x76, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
0x22, 0x00, 0x42, 0x3f, 0x5a, 0x3d, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
0x22, 0x00, 0x42, 0x41, 0x5a, 0x3f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
0x2f, 0x75, 0x6e, 0x69, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x2d, 0x6f, 0x72, 0x67, 0x2f, 0x6d, 0x69,
0x63, 0x72, 0x6f, 0x2d, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x69,
0x63, 0x65, 0x2f, 0x76, 0x33, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x73, 0x65, 0x72, 0x76,
0x69, 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x69, 0x63, 0x65, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@ -1,7 +1,7 @@
syntax = "proto3";
package service;
option go_package="github.com/unistack-org/micro-config-service/v3/proto;service";
option go_package="github.com/unistack-org/micro-config-service/v3/proto;servicepb";
service Config {
rpc Load(LoadRequest) returns (LoadResponse) {};

View File

@ -1,6 +1,6 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
package service
package servicepb
import (
context "context"

View File

@ -63,7 +63,7 @@ func (c *serviceConfig) Load(ctx context.Context) error {
rsp, err := c.client.Load(ctx, &pb.LoadRequest{Service: c.service})
if err != nil && !c.opts.AllowFail {
return fmt.Errorf("failed to load error config: %w", err)
return fmt.Errorf("failed to load config: %w", err)
}
src, err := rutil.Zero(c.opts.Struct)
@ -93,6 +93,17 @@ func (c *serviceConfig) Save(ctx context.Context) error {
}
}
buf, err := c.opts.Codec.Marshal(c.opts.Struct)
if err != nil && c.opts.AllowFail {
return nil
} else if err != nil {
return err
}
if _, err = c.client.Save(ctx, &pb.SaveRequest{Service: c.service, Config: buf}); err != nil && !c.opts.AllowFail {
return fmt.Errorf("failed to save config: %w", err)
}
for _, fn := range c.opts.AfterSave {
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
return err