From b18033c4175a4a641c08f967f4ba952a99b6c8f1 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 9 May 2021 18:47:50 +0300 Subject: [PATCH] implement Save Signed-off-by: Vasiliy Tolstov --- micro/service_micro.pb.go | 2 +- micro/service_micro_rpc.pb.go | 7 ++++--- proto/service.pb.go | 8 ++++---- proto/service.proto | 2 +- proto/service_grpc.pb.go | 2 +- service.go | 13 ++++++++++++- 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/micro/service_micro.pb.go b/micro/service_micro.pb.go index 7410989..f8eb439 100644 --- a/micro/service_micro.pb.go +++ b/micro/service_micro.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-micro // source: service.proto -package service +package servicepb import ( context "context" diff --git a/micro/service_micro_rpc.pb.go b/micro/service_micro_rpc.pb.go index 08aaaa7..68520c2 100644 --- a/micro/service_micro_rpc.pb.go +++ b/micro/service_micro_rpc.pb.go @@ -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...)...)) } diff --git a/proto/service.pb.go b/proto/service.pb.go index 6a2ca40..93af2c1 100644 --- a/proto/service.pb.go +++ b/proto/service.pb.go @@ -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 ( diff --git a/proto/service.proto b/proto/service.proto index 398cade..7ec5c28 100644 --- a/proto/service.proto +++ b/proto/service.proto @@ -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) {}; diff --git a/proto/service_grpc.pb.go b/proto/service_grpc.pb.go index 59a1ed5..2f9a2a6 100644 --- a/proto/service_grpc.pb.go +++ b/proto/service_grpc.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. -package service +package servicepb import ( context "context" diff --git a/service.go b/service.go index fc45c9c..3083b53 100644 --- a/service.go +++ b/service.go @@ -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