From e1a0c68f5c80765a1ab1a01b4e4cc2db4af27e00 Mon Sep 17 00:00:00 2001 From: Luke Tucker Date: Mon, 23 Dec 2024 13:04:58 -0500 Subject: [PATCH] move protobuf adapter onto sqlc type --- diode-server/dbstore/postgres/repository.go | 31 +------------- diode-server/gen/dbstore/postgres/adapters.go | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 30 deletions(-) create mode 100644 diode-server/gen/dbstore/postgres/adapters.go diff --git a/diode-server/dbstore/postgres/repository.go b/diode-server/dbstore/postgres/repository.go index afaffdf..2cd463d 100644 --- a/diode-server/dbstore/postgres/repository.go +++ b/diode-server/dbstore/postgres/repository.go @@ -62,42 +62,13 @@ func (r *Repository) RetrieveIngestionLogByExternalID(ctx context.Context, uuid if err != nil { return nil, nil, err } - log, err := pgToProtoIngestionLog(ingestionLog) + log, err := ingestionLog.ToProtobuf() if err != nil { return nil, nil, err } return &ingestionLog.ID, log, nil } -func pgToProtoIngestionLog(ingestionLog postgres.IngestionLog) (*reconcilerpb.IngestionLog, error) { - entity := &diodepb.Entity{} - if err := protojson.Unmarshal(ingestionLog.Entity, entity); err != nil { - return nil, fmt.Errorf("failed to unmarshal entity: %w", err) - } - var ingestionErr reconcilerpb.IngestionError - if ingestionLog.Error != nil { - if err := protojson.Unmarshal(ingestionLog.Error, &ingestionErr); err != nil { - return nil, fmt.Errorf("failed to unmarshal error: %w", err) - } - } - - log := &reconcilerpb.IngestionLog{ - Id: ingestionLog.ExternalID, - DataType: ingestionLog.DataType.String, - State: reconcilerpb.State(ingestionLog.State.Int32), - RequestId: ingestionLog.RequestID.String, - IngestionTs: ingestionLog.IngestionTs.Int64, - ProducerAppName: ingestionLog.ProducerAppName.String, - ProducerAppVersion: ingestionLog.ProducerAppVersion.String, - SdkName: ingestionLog.SdkName.String, - SdkVersion: ingestionLog.SdkVersion.String, - Entity: entity, - Error: &ingestionErr, - } - - return log, nil -} - // UpdateIngestionLogStateWithError updates an ingestion log with a new state and error. func (r *Repository) UpdateIngestionLogStateWithError(ctx context.Context, id int32, state reconcilerpb.State, ingestionError *reconcilerpb.IngestionError) error { params := postgres.UpdateIngestionLogStateWithErrorParams{ diff --git a/diode-server/gen/dbstore/postgres/adapters.go b/diode-server/gen/dbstore/postgres/adapters.go new file mode 100644 index 0000000..12ba127 --- /dev/null +++ b/diode-server/gen/dbstore/postgres/adapters.go @@ -0,0 +1,40 @@ +package postgres + +import ( + "fmt" + + "google.golang.org/protobuf/encoding/protojson" + + "github.com/netboxlabs/diode/diode-server/gen/diode/v1/diodepb" + "github.com/netboxlabs/diode/diode-server/gen/diode/v1/reconcilerpb" +) + +// ToProtobuf converts sqlc structure to analogous protobuf +func (log IngestionLog) ToProtobuf() (*reconcilerpb.IngestionLog, error) { + entity := &diodepb.Entity{} + if err := protojson.Unmarshal(log.Entity, entity); err != nil { + return nil, fmt.Errorf("failed to unmarshal entity: %w", err) + } + var ingestionErr reconcilerpb.IngestionError + if log.Error != nil { + if err := protojson.Unmarshal(log.Error, &ingestionErr); err != nil { + return nil, fmt.Errorf("failed to unmarshal error: %w", err) + } + } + + pblog := &reconcilerpb.IngestionLog{ + Id: log.ExternalID, + DataType: log.DataType.String, + State: reconcilerpb.State(log.State.Int32), + RequestId: log.RequestID.String, + IngestionTs: log.IngestionTs.Int64, + ProducerAppName: log.ProducerAppName.String, + ProducerAppVersion: log.ProducerAppVersion.String, + SdkName: log.SdkName.String, + SdkVersion: log.SdkVersion.String, + Entity: entity, + Error: &ingestionErr, + } + + return pblog, nil +}