From c8e1fe9f0b299732f3107e7f2db3bc71490ff4cb Mon Sep 17 00:00:00 2001 From: ko_oler <kooler89@gmail.com> Date: Tue, 23 Jan 2024 16:39:40 +0300 Subject: [PATCH] =?UTF-8?q?-=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=B8=D0=BC=D1=8F=20=D0=B8=D0=BC=D0=BF?= =?UTF-8?q?=D0=BE=D1=80=D1=82=D0=B0=20-=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B2?= =?UTF-8?q?=D0=BE=D0=B4=D0=B0=20=D0=B2=20=D1=82=D0=B8=D0=BF=20Any=20-=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B2=20FindOptions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/items/pagination.go | 13 ++++++++----- pkg/log/log.go | 40 ++++++++++++++++++++++++++++------------ pkg/log/service.go | 10 +++++----- pkg/options/options.go | 6 +++--- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/pkg/items/pagination.go b/pkg/items/pagination.go index 754eb22b..936d5b10 100644 --- a/pkg/items/pagination.go +++ b/pkg/items/pagination.go @@ -16,9 +16,12 @@ type BatchProcessor struct { FindPublishedOptions *FindPublishedOptions Filter *Filter - pageSize, pageNum, offset, limit int - sort []string - processed int + // Deprecated использовать offset, limit + pageSize, pageNum int + + offset, limit int + sort []string + processed int } func (b *BatchProcessor) getBatch(ctx context.Context) ([]*Item, bool, error) { @@ -37,7 +40,7 @@ func (b *BatchProcessor) getBatch(ctx context.Context) ([]*Item, bool, error) { Regular: b.FindPublishedOptions.Regular, Hidden: b.FindPublishedOptions.Hidden, Templates: b.FindPublishedOptions.Templates, - FindOptions: *options.NewFindOptions(b.pageNum, b.pageSize, b.offset, b.limit, b.sort...), + FindOptions: *options.NewFindOptions(b.offset, b.limit, b.sort...), }, ) } else { @@ -52,7 +55,7 @@ func (b *BatchProcessor) getBatch(ctx context.Context) ([]*Item, bool, error) { Regular: b.FindOptions.Regular, Hidden: b.FindOptions.Hidden, Templates: b.FindOptions.Templates, - FindOptions: *options.NewFindOptions(b.pageNum, b.pageSize, b.offset, b.limit, b.sort...), + FindOptions: *options.NewFindOptions(b.offset, b.limit, b.sort...), }, ) } diff --git a/pkg/log/log.go b/pkg/log/log.go index 37934c0c..70ee05ee 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -1,24 +1,29 @@ package log import ( + "encoding/json" "time" - "git.perx.ru/perxis/perxis-go/proto/log" + pb "git.perx.ru/perxis/perxis-go/proto/log" + "github.com/golang/protobuf/ptypes/any" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/anypb" "google.golang.org/protobuf/types/known/timestamppb" + wrappers "google.golang.org/protobuf/types/known/wrapperspb" ) type Level int const ( - Info = Level(log.LogLevel_INFO) - Warning = Level(log.LogLevel_WARNING) - Error = Level(log.LogLevel_ERROR) - Critical = Level(log.LogLevel_CRITICAL) - Fatal = Level(log.LogLevel_FATAL) + Info = Level(pb.LogLevel_INFO) + Warning = Level(pb.LogLevel_WARNING) + Error = Level(pb.LogLevel_ERROR) + Critical = Level(pb.LogLevel_CRITICAL) + Fatal = Level(pb.LogLevel_FATAL) ) func (l Level) String() string { - s := log.LogLevel_name[int32(l)] + s := pb.LogLevel_name[int32(l)] if s == "" { s = "UNKNOWN" } @@ -39,24 +44,35 @@ type Entry struct { Tags []string `json:"tags,omitempty" bson:"tags,omitempty"` } -func EntryToPB(entry *Entry) *log.LogEntry { - logEntry := &log.LogEntry{ +func convertInterfaceToAny(v interface{}) (*any.Any, error) { + anyValue := &any.Any{} + bytes, _ := json.Marshal(v) + bytesValue := &wrappers.BytesValue{ + Value: bytes, + } + err := anypb.MarshalFrom(anyValue, bytesValue, proto.MarshalOptions{}) + return anyValue, err +} + +func EntryToPB(entry *Entry) *pb.LogEntry { + logEntry := &pb.LogEntry{ Id: entry.ID, Timestamp: timestamppb.New(entry.Timestamp), - Level: log.LogLevel(entry.LogLevel), + Level: pb.LogLevel(entry.LogLevel), Message: entry.Message, Category: entry.Category, Component: entry.Component, Event: entry.Event, Object: entry.Object, Caller: entry.Caller, - Attr: nil, // todo: как с этим работать? Tags: entry.Tags, } + logEntry.Attr, _ = convertInterfaceToAny(entry.Attr) + return logEntry } -func EntryFromPB(request *log.LogEntry) *Entry { +func EntryFromPB(request *pb.LogEntry) *Entry { return &Entry{ ID: request.Id, Timestamp: request.Timestamp.AsTime(), diff --git a/pkg/log/service.go b/pkg/log/service.go index e555071d..46924932 100644 --- a/pkg/log/service.go +++ b/pkg/log/service.go @@ -5,7 +5,7 @@ import ( itemstransportgrpc "git.perx.ru/perxis/perxis-go/pkg/items/transport/grpc" "git.perx.ru/perxis/perxis-go/pkg/options" - "git.perx.ru/perxis/perxis-go/proto/log" + pb "git.perx.ru/perxis/perxis-go/proto/log" ) type Service interface { @@ -34,12 +34,12 @@ type FindResult struct { Total uint32 } -func FindResultToPB(result *FindResult) *log.FindResult { - findResult := &log.FindResult{ +func FindResultToPB(result *FindResult) *pb.FindResult { + findResult := &pb.FindResult{ Total: result.Total, } - entries := make([]*log.LogEntry, 0, len(result.Entries)) + entries := make([]*pb.LogEntry, 0, len(result.Entries)) for _, e := range result.Entries { entries = append(entries, EntryToPB(e)) } @@ -55,7 +55,7 @@ func FindResultToPB(result *FindResult) *log.FindResult { return findResult } -func FindResultFromPB(result *log.FindResult) *FindResult { +func FindResultFromPB(result *pb.FindResult) *FindResult { findResult := &FindResult{ Total: result.Total, } diff --git a/pkg/options/options.go b/pkg/options/options.go index 8b172de8..277ef203 100644 --- a/pkg/options/options.go +++ b/pkg/options/options.go @@ -34,11 +34,11 @@ type FindOptions struct { } // NewFindOptions создает новые результаты поиска -func NewFindOptions(pageNum, pageSize, offset, limit int, sort ...string) *FindOptions { +func NewFindOptions(offset, limit int, sort ...string) *FindOptions { return &FindOptions{ PaginationOptions: PaginationOptions{ - PageNum: pageNum, - PageSize: pageSize, + PageNum: offset, + PageSize: limit, Offset: offset, Limit: limit, }, -- GitLab