| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package gcpfirestore |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"io" |
| 20 |
"net" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"cloud.google.com/go/firestore" |
| 24 |
pb "cloud.google.com/go/firestore/apiv1/firestorepb" |
| 25 |
"github.com/google/go-cmp/cmp" |
| 26 |
"google.golang.org/api/option" |
| 27 |
"google.golang.org/grpc" |
| 28 |
"google.golang.org/grpc/credentials/insecure" |
| 29 |
"google.golang.org/grpc/metadata" |
| 30 |
tspb "google.golang.org/protobuf/types/known/timestamppb" |
| 31 |
) |
| 32 |
|
| 33 |
// A nativeCodec encodes and decodes structs using the cloud.google.com/go/firestore |
| 34 |
// client. Since that package doesn't export its codec, we have to go behind the |
| 35 |
// scenes and intercept traffic at the gRPC level. We use interceptors to do that. (A |
| 36 |
// mock server would have worked too.) |
| 37 |
type nativeCodec struct { |
| 38 |
client *firestore.Client |
| 39 |
doc *pb.Document |
| 40 |
} |
| 41 |
|
| 42 |
func newNativeCodec() (*nativeCodec, error) { |
| 43 |
// Establish a gRPC server, just so we have a connection to hang the interceptors on. |
| 44 |
srv := grpc.NewServer() |
| 45 |
l, err := net.Listen("tcp", "127.0.0.1:0") |
| 46 |
if err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
go func() { |
| 50 |
if err := srv.Serve(l); err != nil { |
| 51 |
panic(err) // we should never get an error because we just connect and stop |
| 52 |
} |
| 53 |
}() |
| 54 |
nc := &nativeCodec{} |
| 55 |
|
| 56 |
conn, err := grpc.Dial(l.Addr().String(), |
| 57 |
grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 58 |
grpc.WithBlock(), |
| 59 |
grpc.WithUnaryInterceptor(nc.interceptUnary), |
| 60 |
grpc.WithStreamInterceptor(nc.interceptStream)) |
| 61 |
if err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
conn.Close() |
| 65 |
srv.Stop() |
| 66 |
nc.client, err = firestore.NewClient(context.Background(), "P", option.WithGRPCConn(conn)) |
| 67 |
if err != nil { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
return nc, nil |
| 71 |
} |
| 72 |
|
| 73 |
// Intercept all unary (non-streaming) RPCs. The only one we should ever get is a Commit, for |
| 74 |
// the Create call in Encode. |
| 75 |
// If this completes successfully, the encoded *pb.Document will be in c.doc. |
| 76 |
func (c *nativeCodec) interceptUnary(_ context.Context, method string, req, res any, _ *grpc.ClientConn, _ grpc.UnaryInvoker, _ ...grpc.CallOption) error { |
| 77 |
c.doc = req.(*pb.CommitRequest).Writes[0].GetUpdate() |
| 78 |
res.(*pb.CommitResponse).WriteResults = []*pb.WriteResult{{}} |
| 79 |
return nil |
| 80 |
} |
| 81 |
|
| 82 |
// Intercept all streaming RPCs. The only one we should ever get is a BatchGet, for the Get |
| 83 |
// call in Decode. |
| 84 |
// Before this is called, c.doc must be set to the *pb.Document to be returned from the call. |
| 85 |
func (c *nativeCodec) interceptStream(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { |
| 86 |
return &clientStream{ctx: ctx, doc: c.doc}, nil |
| 87 |
} |
| 88 |
|
| 89 |
// clientStream is a fake client stream. It returns a single document, then terminates. |
| 90 |
type clientStream struct { |
| 91 |
ctx context.Context |
| 92 |
doc *pb.Document |
| 93 |
} |
| 94 |
|
| 95 |
func (cs *clientStream) RecvMsg(m any) error { |
| 96 |
if cs.doc != nil { |
| 97 |
cs.doc.CreateTime = &tspb.Timestamp{} |
| 98 |
cs.doc.UpdateTime = &tspb.Timestamp{} |
| 99 |
m.(*pb.BatchGetDocumentsResponse).Result = &pb.BatchGetDocumentsResponse_Found{Found: cs.doc} |
| 100 |
cs.doc = nil |
| 101 |
return nil |
| 102 |
} |
| 103 |
return io.EOF |
| 104 |
} |
| 105 |
|
| 106 |
func (cs *clientStream) Context() context.Context { return cs.ctx } |
| 107 |
func (cs *clientStream) SendMsg(m any) error { return nil } |
| 108 |
func (cs *clientStream) Header() (metadata.MD, error) { return nil, nil } |
| 109 |
func (cs *clientStream) Trailer() metadata.MD { return nil } |
| 110 |
func (cs *clientStream) CloseSend() error { return nil } |
| 111 |
|
| 112 |
// Encode a Go value into a Firestore proto document. |
| 113 |
func (c *nativeCodec) Encode(x any) (*pb.Document, error) { |
| 114 |
_, err := c.client.Collection("C").Doc("D").Create(context.Background(), x) |
| 115 |
if err != nil { |
| 116 |
return nil, err |
| 117 |
} |
| 118 |
return c.doc, nil |
| 119 |
} |
| 120 |
|
| 121 |
// Decode value, which must be a *pb.Document, into dest. |
| 122 |
func (c *nativeCodec) Decode(value *pb.Document, dest any) error { |
| 123 |
c.doc = value |
| 124 |
docsnap, err := c.client.Collection("C").Doc("D").Get(context.Background()) |
| 125 |
if err != nil { |
| 126 |
return err |
| 127 |
} |
| 128 |
return docsnap.DataTo(dest) |
| 129 |
} |
| 130 |
|
| 131 |
func TestNativeCodec(t *testing.T) { |
| 132 |
nc, err := newNativeCodec() |
| 133 |
if err != nil { |
| 134 |
t.Fatal(err) |
| 135 |
} |
| 136 |
type S struct { |
| 137 |
A int |
| 138 |
} |
| 139 |
want := S{3} |
| 140 |
fields, err := nc.Encode(&want) |
| 141 |
if err != nil { |
| 142 |
t.Fatal(err) |
| 143 |
} |
| 144 |
var got S |
| 145 |
if err := nc.Decode(fields, &got); err != nil { |
| 146 |
t.Fatal(err) |
| 147 |
} |
| 148 |
if !cmp.Equal(got, want) { |
| 149 |
t.Errorf("got %+v, want %+v", got, want) |
| 150 |
} |
| 151 |
} |