package transformers import ( "encoding/json" "fmt" "time" "git.coopgo.io/coopgo-platform/saved-search/data/types" "git.coopgo.io/coopgo-platform/saved-search/servers/grpc/proto/gen" "github.com/paulmach/orb/geojson" "google.golang.org/protobuf/types/known/structpb" "google.golang.org/protobuf/types/known/timestamppb" ) // SavedSearchTypeToProto converts a domain SavedSearch to protobuf SavedSearch func SavedSearchTypeToProto(search *types.SavedSearch) (*gen.SavedSearch, error) { if search == nil { return nil, fmt.Errorf("search cannot be nil") } departure, err := GeoJsonFeatureToProto(search.Departure) if err != nil { return nil, fmt.Errorf("failed to convert departure: %w", err) } destination, err := GeoJsonFeatureToProto(search.Destination) if err != nil { return nil, fmt.Errorf("failed to convert destination: %w", err) } data, err := structpb.NewStruct(search.Data) if err != nil { return nil, fmt.Errorf("failed to convert data: %w", err) } return &gen.SavedSearch{ Id: search.ID, OwnerId: search.OwnerID, Departure: departure, Destination: destination, Datetime: timestamppb.New(search.DateTime), Data: data, CreatedAt: timestamppb.New(search.CreatedAt), UpdatedAt: timestamppb.New(search.UpdatedAt), }, nil } // SavedSearchProtoToType converts a protobuf SavedSearch to domain SavedSearch func SavedSearchProtoToType(search *gen.SavedSearch) (*types.SavedSearch, error) { if search == nil { return nil, fmt.Errorf("search cannot be nil") } departure, err := GeoJsonFeatureFromProto(search.Departure) if err != nil { return nil, fmt.Errorf("failed to convert departure: %w", err) } destination, err := GeoJsonFeatureFromProto(search.Destination) if err != nil { return nil, fmt.Errorf("failed to convert destination: %w", err) } var data map[string]interface{} if search.Data != nil { data = search.Data.AsMap() } else { data = make(map[string]interface{}) } var datetime time.Time if search.Datetime != nil { datetime = search.Datetime.AsTime() } return &types.SavedSearch{ ID: search.Id, OwnerID: search.OwnerId, Departure: departure, Destination: destination, DateTime: datetime, Data: data, CreatedAt: search.CreatedAt.AsTime(), UpdatedAt: search.UpdatedAt.AsTime(), }, nil } // GeoJsonFeatureToProto converts a GeoJSON Feature to protobuf SavedSearchGeoJsonFeature func GeoJsonFeatureToProto(feature *geojson.Feature) (*gen.SavedSearchGeoJsonFeature, error) { if feature == nil { return nil, fmt.Errorf("feature cannot be nil") } serialized, err := json.Marshal(feature) if err != nil { return nil, fmt.Errorf("failed to serialize feature: %w", err) } return &gen.SavedSearchGeoJsonFeature{ Serialized: string(serialized), }, nil } // GeoJsonFeatureFromProto converts a protobuf SavedSearchGeoJsonFeature to GeoJSON Feature func GeoJsonFeatureFromProto(protoFeature *gen.SavedSearchGeoJsonFeature) (*geojson.Feature, error) { if protoFeature == nil { return nil, fmt.Errorf("proto feature cannot be nil") } var feature geojson.Feature if err := json.Unmarshal([]byte(protoFeature.Serialized), &feature); err != nil { return nil, fmt.Errorf("failed to deserialize feature: %w", err) } return &feature, nil }