-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move protobuf adapter onto sqlc type
- Loading branch information
Showing
2 changed files
with
41 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |