Skip to content

Commit

Permalink
move protobuf adapter onto sqlc type
Browse files Browse the repository at this point in the history
  • Loading branch information
ltucker committed Dec 23, 2024
1 parent debebfa commit e1a0c68
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 30 deletions.
31 changes: 1 addition & 30 deletions diode-server/dbstore/postgres/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
40 changes: 40 additions & 0 deletions diode-server/gen/dbstore/postgres/adapters.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e1a0c68

Please sign in to comment.