Skip to content

Commit

Permalink
Merge pull request #76 from nitrictech/feature/document-service
Browse files Browse the repository at this point in the history
Feature/document service
  • Loading branch information
medgar-nitric authored Jul 14, 2021
2 parents 6f737cf + 119d002 commit b53477e
Show file tree
Hide file tree
Showing 70 changed files with 3,940 additions and 2,891 deletions.
31 changes: 30 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,33 @@ jobs:
- name: Run Tests
run: make test
- name: Check Dependency Licenses
run: make license-check
run: make license-check

# Run integration tests
test-integration:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
token: ${{secrets.GOLANG_TOKEN}}
submodules: recursive
- name: Setup Go
uses: actions/setup-go@v2-beta
- name: Install Protoc
uses: arduino/setup-protoc@v1
- name: Install modules
run: make install-tools
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Setup local DynamoDB
run: make install-test-tools
- name: Setup Cloud SDK
uses: google-github-actions/setup-gcloud@master
- name: Install Google Cloud SDK components
run: yes | gcloud components install beta cloud-firestore-emulator
- name: Run Integration Tests
run: make test-integration
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ provider as an alternative to the fixed set of plugins in the static membranes.
- Make
- Docker
- Google Protocol Buffers Compiler
- Google Cloud SDK (for Firestore testing)
- JRE (for DynamoDB testing)

### Getting Started

Expand All @@ -95,12 +97,27 @@ git submodule update --init --recursive
make install-tools
```

##### Protocol Buffers
#### Install integration testing tools
```bash
make install-test-tools
```

##### Install Protocol Buffers
Download the Google Protobuf Compiler (standalone binary called `protoc`) from https://github.com/protocolbuffers/protobuf and add it to your $PATH.

> On Mac OS with Homebrew, you can run `brew install protobuf`
#### Run tests
##### Install Google Cloud SDK
Install the Google Cloud SDK following in the instructions at: https://cloud.google.com/sdk/docs/install

#### Install JRE
Install a Java Runtime Environment (JRE) version 11 or later for your OS. For example on Ubuntu Linux run:

```bash
sudo apt-get install openjdk-11-jdk
```

### Run tests
```bash
make tests
```
Expand Down
62 changes: 0 additions & 62 deletions adapters/grpc/auth_grpc.go

This file was deleted.

171 changes: 171 additions & 0 deletions adapters/grpc/document_grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Copyright 2021 Nitric Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package grpc

import (
"context"
pb "github.com/nitric-dev/membrane/interfaces/nitric/v1"
"github.com/nitric-dev/membrane/sdk"
"google.golang.org/protobuf/types/known/structpb"
)

// DocumentServer - GRPC Interface for registered Nitric Document Plugin
type DocumentServer struct {
pb.UnimplementedDocumentServiceServer
// TODO: Support multiple plugin registrations
// Just need to settle on a way of addressing them on calls
documentPlugin sdk.DocumentService
}

func (s *DocumentServer) Set(ctx context.Context, req *pb.DocumentSetRequest) (*pb.DocumentSetResponse, error) {
key := keyFromWire(req.Key)

err := s.documentPlugin.Set(key, req.GetContent().AsMap())
if err != nil {
return nil, err
}

return &pb.DocumentSetResponse{}, nil
}

func (s *DocumentServer) Get(ctx context.Context, req *pb.DocumentGetRequest) (*pb.DocumentGetResponse, error) {
key := keyFromWire(req.Key)

doc, err := s.documentPlugin.Get(key)
if err != nil {
return nil, err
}

pbDoc, err := documentToWire(doc)
if err != nil {
return nil, err
}

return &pb.DocumentGetResponse{
Document: pbDoc,
}, nil
}

func (s *DocumentServer) Delete(ctx context.Context, req *pb.DocumentDeleteRequest) (*pb.DocumentDeleteResponse, error) {
key := keyFromWire(req.Key)

err := s.documentPlugin.Delete(key)
if err != nil {
return nil, err
}

return &pb.DocumentDeleteResponse{}, nil
}

func (s *DocumentServer) Query(ctx context.Context, req *pb.DocumentQueryRequest) (*pb.DocumentQueryResponse, error) {
collection := collectionFromWire(req.Collection)
expressions := make([]sdk.QueryExpression, len(req.GetExpressions()))
for i, exp := range req.GetExpressions() {
expressions[i] = sdk.QueryExpression{
Operand: exp.GetOperand(),
Operator: exp.GetOperator(),
Value: toExpValue(exp.GetValue()),
}
}
limit := int(req.GetLimit())
pagingMap := req.GetPagingToken()

qr, err := s.documentPlugin.Query(collection, expressions, limit, pagingMap)
if err != nil {
return nil, err
}

pbDocuments := make([]*pb.Document, len(qr.Documents))
for _, doc := range qr.Documents {
pbDoc, err := documentToWire(&doc)
if err != nil {
return nil, err
}

pbDocuments = append(pbDocuments, pbDoc)
}

return &pb.DocumentQueryResponse{
Documents: pbDocuments,
PagingToken: qr.PagingToken,
}, nil
}

func NewDocumentServer(docPlugin sdk.DocumentService) pb.DocumentServiceServer {
return &DocumentServer{
documentPlugin: docPlugin,
}
}

func documentToWire(doc *sdk.Document) (*pb.Document, error) {
valStruct, err := structpb.NewStruct(doc.Content)
if err != nil {
return nil, err
}

return &pb.Document{
Content: valStruct,
}, nil
}

// keyFromWire - returns an Membrane SDK Document Key from the protobuf wire representation
// recursively calls collectionFromWire for the document's collection and parents if present
func keyFromWire(key *pb.Key) *sdk.Key {
if key == nil {
return nil
}

sdkKey := &sdk.Key{
Collection: collectionFromWire(key.GetCollection()),
Id: key.GetId(),
}

return sdkKey
}

// collectionFromWire - returns an Membrane SDK Document Collection from the protobuf wire representation
// recursively calls keyFromWire if the collection is a sub-collection under another key
func collectionFromWire(coll *pb.Collection) *sdk.Collection {
if coll == nil {
return nil
}

if coll.Parent == nil {
return &sdk.Collection{
Name: coll.Name,
}
} else {
return &sdk.Collection{
Name: coll.Name,
Parent: keyFromWire(coll.Parent),
}
}
}

func toExpValue(x *pb.ExpressionValue) interface{} {
if x, ok := x.GetKind().(*pb.ExpressionValue_IntValue); ok {
return x.IntValue
}
if x, ok := x.GetKind().(*pb.ExpressionValue_DoubleValue); ok {
return x.DoubleValue
}
if x, ok := x.GetKind().(*pb.ExpressionValue_StringValue); ok {
return x.StringValue
}
if x, ok := x.GetKind().(*pb.ExpressionValue_BoolValue); ok {
return x.BoolValue
}
return nil
}
2 changes: 1 addition & 1 deletion contracts
31 changes: 12 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,37 @@ require (
cloud.google.com/go/firestore v1.5.0
cloud.google.com/go/pubsub v1.3.1
cloud.google.com/go/storage v1.10.0
firebase.google.com/go v3.13.0+incompatible
firebase.google.com/go/v4 v4.5.0
github.com/Azure/azure-sdk-for-go v51.3.0+incompatible
github.com/Azure/go-autorest/autorest v0.11.18 // indirect
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/DataDog/zstd v1.4.8 // indirect
github.com/Knetic/govaluate v3.0.0+incompatible
github.com/Sereal/Sereal v0.0.0-20200820125258-a016b7cda3f3 // indirect
github.com/asdine/storm v2.1.2+incompatible
github.com/aws/aws-lambda-go v1.20.0
github.com/aws/aws-sdk-go v1.36.12
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/garyburd/redigo v1.6.2 // indirect
github.com/golang/protobuf v1.5.2
github.com/google/addlicense v0.0.0-20210428195630-6d92264d7170 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/addlicense v0.0.0-20210428195630-6d92264d7170
github.com/google/uuid v1.1.2
github.com/googleapis/gax-go/v2 v2.0.5
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 // indirect
github.com/mitchellh/golicense v0.2.0 // indirect
github.com/mitchellh/mapstructure v1.4.1
github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.10.1
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/uw-labs/lichen v0.1.4 // indirect
github.com/uw-labs/lichen v0.1.4
github.com/valyala/fasthttp v1.23.0
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
github.com/vmihailenco/msgpack v3.3.3+incompatible // indirect
go.etcd.io/bbolt v1.3.5
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/tools v0.1.2 // indirect
golang.org/x/tools v0.1.5
google.golang.org/api v0.40.0
google.golang.org/genproto v0.0.0-20210222152913-aa3ee6e6a81c
google.golang.org/grpc v1.35.0
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0
google.golang.org/protobuf v1.26.0
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit b53477e

Please sign in to comment.