-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01fa66c
commit 7a4577a
Showing
75 changed files
with
4,209 additions
and
2,806 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
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 was deleted.
Oops, something went wrong.
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,166 @@ | ||
// 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/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
// GRPC Interface for registered Nitric Document Plugin | ||
type DocumentServer struct { | ||
pb.UnimplementedDocumentServer | ||
// TODO: Support multiple plugin registrations | ||
// Just need to settle on a way of addressing them on calls | ||
documentPlugin sdk.DocumentService | ||
} | ||
|
||
func (s *DocumentServer) checkPluginRegistered() (bool, error) { | ||
if s.documentPlugin == nil { | ||
return false, status.Errorf(codes.Unimplemented, "Document plugin not registered") | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (s *DocumentServer) Set(ctx context.Context, req *pb.DocSetRequest) (*pb.DocSetResponse, error) { | ||
if ok, err := s.checkPluginRegistered(); ok { | ||
key := toSdkKey(req.Key) | ||
subKey := toSdkKey(req.SubKey) | ||
|
||
if err := s.documentPlugin.Set(*key, subKey, req.GetValue().AsMap()); err == nil { | ||
return &pb.DocSetResponse{}, nil | ||
} else { | ||
// Case: Failed to create the key | ||
// TODO: Translate from internal Document Service Error | ||
return nil, err | ||
} | ||
} else { | ||
// Case: Plugin was not registered | ||
return nil, err | ||
} | ||
} | ||
|
||
func (s *DocumentServer) Get(ctx context.Context, req *pb.DocGetRequest) (*pb.DocGetResponse, error) { | ||
if ok, err := s.checkPluginRegistered(); ok { | ||
key := toSdkKey(req.Key) | ||
subKey := toSdkKey(req.SubKey) | ||
|
||
if val, err := s.documentPlugin.Get(*key, subKey); err == nil { | ||
if valStruct, err := structpb.NewStruct(val); err == nil { | ||
return &pb.DocGetResponse{ | ||
Value: valStruct, | ||
}, nil | ||
} else { | ||
// Case: Failed to create PB struct from stored value | ||
// TODO: Translate from internal Document Plugin Error | ||
return nil, err | ||
} | ||
} else { | ||
// Case: There was an error retrieving the keyvalue | ||
// TODO: Translate from internal Document Plugin Error | ||
return nil, err | ||
} | ||
} else { | ||
// Case: The Document plugin was not registered | ||
// TODO: Translate from internal Document Plugin Error | ||
return nil, err | ||
} | ||
} | ||
|
||
func (s *DocumentServer) Delete(ctx context.Context, req *pb.DocDeleteRequest) (*pb.DocDeleteResponse, error) { | ||
if ok, err := s.checkPluginRegistered(); ok { | ||
key := toSdkKey(req.Key) | ||
subKey := toSdkKey(req.SubKey) | ||
|
||
if err := s.documentPlugin.Delete(*key, subKey); err == nil { | ||
return &pb.DocDeleteResponse{}, nil | ||
} else { | ||
// Case: Failed to create the keyvalue | ||
// TODO: Translate from internal Document Plugin Error | ||
return nil, err | ||
} | ||
} else { | ||
// Case: Plugin was not registered | ||
return nil, err | ||
} | ||
} | ||
|
||
func (s *DocumentServer) Query(ctx context.Context, req *pb.DocQueryRequest) (*pb.DocQueryResponse, error) { | ||
if ok, err := s.checkPluginRegistered(); ok { | ||
key := toSdkKey(req.Key) | ||
subcoll := req.GetSubCollection() | ||
expressions := make([]sdk.QueryExpression, len(req.GetExpressions())) | ||
for i, exp := range req.GetExpressions() { | ||
expressions[i] = sdk.QueryExpression{ | ||
Operand: exp.GetOperand(), | ||
Operator: exp.GetOperator(), | ||
Value: exp.GetValue(), | ||
} | ||
} | ||
limit := int(req.GetLimit()) | ||
pagingMap := req.GetPagingToken() | ||
|
||
if qr, err := s.documentPlugin.Query(*key, subcoll, expressions, limit, pagingMap); err == nil { | ||
|
||
valStructs := make([]*structpb.Struct, len(qr.Data)) | ||
for i, valMap := range qr.Data { | ||
if valStruct, err := structpb.NewStruct(valMap); err == nil { | ||
valStructs[i] = valStruct | ||
|
||
} else { | ||
// Case: Failed to create PB struct from stored value | ||
// TODO: Translate from internal Document Plugin Error | ||
return nil, err | ||
} | ||
} | ||
|
||
return &pb.DocQueryResponse{ | ||
Values: valStructs, | ||
PagingToken: qr.PagingToken, | ||
}, nil | ||
|
||
} else { | ||
// Case: Failed to create the keyvalue | ||
// TODO: Translate from internal Document Plugin Error | ||
return nil, err | ||
} | ||
|
||
} else { | ||
// Case: Plugin was not registered | ||
return nil, err | ||
} | ||
} | ||
|
||
func NewDocumentServer(docPlugin sdk.DocumentService) pb.DocumentServer { | ||
return &DocumentServer{ | ||
documentPlugin: docPlugin, | ||
} | ||
} | ||
|
||
func toSdkKey(key *pb.Key) *sdk.Key { | ||
if key != nil { | ||
return &sdk.Key{ | ||
Collection: key.GetCollection(), | ||
Id: key.GetId(), | ||
} | ||
} | ||
return nil | ||
} |
Submodule contracts
updated
from 626788 to 8532f4
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
Oops, something went wrong.