Skip to content

Commit

Permalink
Merge pull request #18 from cldcvr/fix/linting_issues
Browse files Browse the repository at this point in the history
fix : issues related linting fixed
  • Loading branch information
ankitmalikg2 authored Nov 17, 2020
2 parents 7b1d3b6 + 6a5cfbb commit fe7d852
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 51 deletions.
11 changes: 11 additions & 0 deletions api/v1/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ func extractOperations(updateExpression string) map[string]string {
return ops
}

// ReplaceHashRangeExpr replaces the attribute names from Filter Expression and Range Expression
func ReplaceHashRangeExpr(query models.Query) models.Query {
for k, v := range query.ExpressionAttributeNames {
query.FilterExp = strings.ReplaceAll(query.FilterExp, k, v)
Expand All @@ -346,6 +347,7 @@ func ReplaceHashRangeExpr(query models.Query) models.Query {
return query
}

// ConvertDynamoToMap converts the Dynamodb Object to Map
func ConvertDynamoToMap(tableName string, dynamoMap map[string]*dynamodb.AttributeValue) (map[string]interface{}, error) {
if dynamoMap == nil || len(dynamoMap) == 0 {
return nil, nil
Expand All @@ -362,6 +364,7 @@ func ConvertDynamoToMap(tableName string, dynamoMap map[string]*dynamodb.Attribu
return rs, nil
}

// ConvertDynamoArrayToMapArray this converts Dynamodb Object Array into Map Array
func ConvertDynamoArrayToMapArray(tableName string, dynamoMap []map[string]*dynamodb.AttributeValue) ([]map[string]interface{}, error) {
if dynamoMap == nil || len(dynamoMap) == 0 {
return nil, nil
Expand All @@ -380,6 +383,7 @@ func ConvertDynamoArrayToMapArray(tableName string, dynamoMap []map[string]*dyna
return rs, nil
}

// ChangeColumnToSpannerExpressionName converts the Column Name into Spanner equivalent
func ChangeColumnToSpannerExpressionName(tableName string, expressNameMap map[string]string) map[string]string {
_, ok := models.TableColChangeMap[tableName]
if !ok {
Expand All @@ -400,6 +404,7 @@ func ChangeColumnToSpannerExpressionName(tableName string, expressNameMap map[st
return rs
}

// ChangesArrayResponseToOriginalColumns changes the spanner column names to original column names
func ChangesArrayResponseToOriginalColumns(tableName string, obj []map[string]interface{}) []map[string]interface{} {
_, ok := models.TableColChangeMap[tableName]
if !ok {
Expand All @@ -411,6 +416,7 @@ func ChangesArrayResponseToOriginalColumns(tableName string, obj []map[string]in
return obj
}

// ChangeResponseToOriginalColumns converts the map of spanner column into original column names
func ChangeResponseToOriginalColumns(tableName string, obj map[string]interface{}) map[string]interface{} {
_, ok := models.TableColChangeMap[tableName]
if !ok {
Expand All @@ -432,6 +438,7 @@ func ChangeResponseToOriginalColumns(tableName string, obj map[string]interface{
return rs
}

// ChangeResponseColumn changes the spanner column name into original column if those exists
func ChangeResponseColumn(obj map[string]interface{}) map[string]interface{} {
rs := make(map[string]interface{})

Expand All @@ -448,6 +455,8 @@ func ChangeResponseColumn(obj map[string]interface{}) map[string]interface{} {

return rs
}

// ChangeColumnToSpanner converts original column name to spanner supported column names
func ChangeColumnToSpanner(obj map[string]interface{}) map[string]interface{} {
rs := make(map[string]interface{})

Expand Down Expand Up @@ -533,6 +542,7 @@ func convertFrom(a *dynamodb.AttributeValue, tableName string) interface{} {
panic(fmt.Sprintf("%#v is not a supported dynamodb.AttributeValue", a))
}

// ConvertFromMap converts dynamodb AttributeValue into interface
func ConvertFromMap(item map[string]*dynamodb.AttributeValue, v interface{}, tableName string) (err error) {
defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -605,6 +615,7 @@ func isTyped(v reflect.Type) bool {
return false
}

// ChangeQueryResponseColumn changes the response into dynamodb response for Query api
func ChangeQueryResponseColumn(tableName string, obj map[string]interface{}) map[string]interface{} {
_, ok := models.TableColChangeMap[tableName]
if !ok {
Expand Down
6 changes: 3 additions & 3 deletions api/v1/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/opentracing/opentracing-go"
)

// InitVPC - Init VPC routes
// InitDBAPI - routes for apis
func InitDBAPI(g *gin.RouterGroup) {

r := g.Group("/")
Expand Down Expand Up @@ -240,7 +240,7 @@ func QueryTable(c *gin.Context) {
}
}

// GetMetaWithProjection to get with projections
// GetItemMeta to get with projections
// @Description Get a record with projections
// @Summary Get a record with projections
// @ID get-with-projection
Expand Down Expand Up @@ -295,7 +295,7 @@ func GetItemMeta(c *gin.Context) {
}
}

// BatchGetMetaWithProjection to get with projections
// BatchGetItem to get with projections
// @Description Request items in a batch with projections.
// @Summary Request items in a batch with projections.
// @ID batch-get-with-projection
Expand Down
1 change: 1 addition & 0 deletions api/v1/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/gin-gonic/gin"
)

// PanicHandler is global handler for all type of panic
func PanicHandler(c *gin.Context) {
if e := recover(); e != nil {
stack := string(debug.Stack())
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (

// Configuration struct
type Configuration struct {
GOOGLE_PROJECT_ID string
SPANNER_DB string
GoogleProjectID string
SpannerDb string
}

var once sync.Once
Expand Down
37 changes: 0 additions & 37 deletions constant/constant.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/valyala/fasthttp v1.15.1 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
go.uber.org/zap v1.15.0
golang.org/x/tools v0.0.0-20201117021029-3c3a81204b10 // indirect
google.golang.org/api v0.29.0
google.golang.org/genproto v0.0.0-20200711021454-869866162049
gopkg.in/go-playground/assert.v1 v1.2.1
Expand Down
15 changes: 13 additions & 2 deletions models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ type ScanMeta struct {
ExpressionAttributeValues map[string]*dynamodb.AttributeValue `json:"ExpressionAttributeValues"`
}

// TableConfig for Configuration table
type TableConfig struct {
PartitionKey string `json:"partitionKey,omitempty"`
SortKey string `json:"sortKey,omitempty"`
Expand Down Expand Up @@ -193,9 +194,13 @@ var TableDDL map[string]map[string]string
// TableColumnMap - this contains the list of columns for the tables
var TableColumnMap map[string][]string

// TableColChangeMap for changed columns map
var TableColChangeMap map[string]struct{}

// ColumnToOriginalCol for Original column map
var ColumnToOriginalCol map[string]string

// OriginalColResponse for Original Column Response
var OriginalColResponse map[string]string

func init() {
Expand All @@ -210,15 +215,16 @@ func init() {
OriginalColResponse = make(map[string]string)
}

// Eval for Evaluation expression
type Eval struct {
// Cond conditions.Expr
Cond *vm.Program
Attributes []string
Cols []string
Tokens []string
ValueMap map[string]interface{}
}

// UpdateExpressionCondition for Update Condition
type UpdateExpressionCondition struct {
Field []string
Value []string
Expand All @@ -227,12 +233,13 @@ type UpdateExpressionCondition struct {
AddValues map[string]float64
}

type dynamodb_adapter_table_ddl struct {
type dynamodbAdapterTableDdl struct {
Table string
Column string
DataType string
}

// DBAudit for db auditing data
type DBAudit struct {
ID string `json:"id,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
Expand All @@ -247,6 +254,7 @@ type DBAudit struct {
Payload string `json:"payload"`
}

// ConfigControllerModel for Config controller
type ConfigControllerModel struct {
Mux sync.RWMutex
UniqueVal string
Expand All @@ -258,8 +266,10 @@ type ConfigControllerModel struct {
PubSubTopic map[string]string
}

// ConfigController object for ConfigControllerModel
var ConfigController *ConfigControllerModel

// SpannerTableMap for spanner column map
var SpannerTableMap = make(map[string]string)

func init() {
Expand All @@ -272,6 +282,7 @@ func init() {
ConfigController.PubSubTopic = make(map[string]string)
}

// StreamDataModel for streaming data
type StreamDataModel struct {
OldImage map[string]interface{} `json:"oldImage"`
NewImage map[string]interface{} `json:"newImage"`
Expand Down
4 changes: 4 additions & 0 deletions service/services/config-control.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ var ctx = context.Background()

var c *cron.Cron

// StartConfigManager starts config mangager for fetching the config manager data after certain time
func StartConfigManager() {
c = cron.New()
c.AddFunc("@every "+models.ConfigController.CornTime+"m", fetchConfigData)
c.Start()
fetchConfigData()
}

// MayIReadOrWrite for checking the operation is allowed or not
func MayIReadOrWrite(table string, IsMutation bool, operation string) bool {
return true
}
Expand Down Expand Up @@ -156,13 +158,15 @@ func parseConfig(table string, config string, count int) {
}
}

// IsMyStreamEnabled checks if a table is enabaled for streaming or not
func IsMyStreamEnabled(tableName string) bool {
models.ConfigController.Mux.RLock()
defer models.ConfigController.Mux.RUnlock()
_, ok := models.ConfigController.StreamEnable[tableName]
return ok
}

// IsPubSubAllowed to check if PubSub is allowed or not for a table
func IsPubSubAllowed(tableName string) (string, bool) {
models.ConfigController.Mux.RLock()
defer models.ConfigController.Mux.RUnlock()
Expand Down
7 changes: 4 additions & 3 deletions service/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func Put(ctx context.Context, tableName string, putObj map[string]interface{}, e
return updateResp, nil
}

//Add checks the expression for converting the data
func Add(ctx context.Context, tableName string, attrMap map[string]interface{}, condExpression string, m, expressionAttr map[string]interface{}, expr *models.UpdateExpressionCondition, oldRes map[string]interface{}) (map[string]interface{}, error) {
tableConf, err := config.GetTableConf(tableName)
if err != nil {
Expand Down Expand Up @@ -141,6 +142,7 @@ func Add(ctx context.Context, tableName string, attrMap map[string]interface{},
return updateResp, nil
}

//Del checks the expression for saving the data
func Del(ctx context.Context, tableName string, attrMap map[string]interface{}, condExpression string, expressionAttr map[string]interface{}, expr *models.UpdateExpressionCondition) (map[string]interface{}, error) {
logger.LogDebug(expressionAttr)
tableConf, err := config.GetTableConf(tableName)
Expand Down Expand Up @@ -168,7 +170,7 @@ func Del(ctx context.Context, tableName string, attrMap map[string]interface{},
return res, nil
}

// BatchGet get table data
// BatchGet for batch operation for getting data
func BatchGet(ctx context.Context, tableName string, keyMapArray []map[string]interface{}) ([]map[string]interface{}, error) {
if len(keyMapArray) == 0 {
var resp = make([]map[string]interface{}, 0)
Expand Down Expand Up @@ -631,6 +633,7 @@ func scanSpanerTable(ctx context.Context, tableName, pKey, sKey string) ([]map[s
return result, nil
}

// Remove for remove operation in update
func Remove(ctx context.Context, tableName string, updateAttr models.UpdateAttr, actionValue string, expr *models.UpdateExpressionCondition, oldRes map[string]interface{}) (map[string]interface{}, error) {
actionValue = strings.ReplaceAll(actionValue, " ", "")
colsToRemove := strings.Split(actionValue, ",")
Expand Down Expand Up @@ -659,6 +662,4 @@ func Remove(ctx context.Context, tableName string, updateAttr models.UpdateAttr,
delete(updateResp, colsToRemove[i])
}
return updateResp, nil

return nil, errors.New("ResourceNotFoundException")
}
6 changes: 4 additions & 2 deletions service/services/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ var pubsubClient *pubsub.Client
var mClients = map[string]*pubsub.Topic{}
var mux = &sync.Mutex{}

// InitStream for initializing the stream
func InitStream() {
var err error
pubsubClient, err = pubsub.NewClient(context.Background(), config.ConfigurationMap.GOOGLE_PROJECT_ID)
pubsubClient, err = pubsub.NewClient(context.Background(), config.ConfigurationMap.GoogleProjectID)
if err != nil {
logger.LogFatal(err)
}
}

// StreamDataToThirdParty for streaming data to any third party source
func StreamDataToThirdParty(oldImage, newImage map[string]interface{}, tableName string) {
if !IsMyStreamEnabled(tableName) {
return
Expand Down Expand Up @@ -93,7 +95,7 @@ func pubsubPublish(streamObj *models.StreamDataModel) {
topic, ok := mClients[topicName]
if !ok {
topic = pubsubClient.
TopicInProject(topicName, config.ConfigurationMap.GOOGLE_PROJECT_ID)
TopicInProject(topicName, config.ConfigurationMap.GoogleProjectID)
mClients[topicName] = topic
}
message := &pubsub.Message{}
Expand Down
3 changes: 2 additions & 1 deletion storage/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func ReadDatabaseSchema() ([]string, error) {
var statments []string
for k := range m {
req := &databasepb.GetDatabaseDdlRequest{}
req.Database = "projects/" + config.ConfigurationMap.GOOGLE_PROJECT_ID + "/instances/" + k + "/databases/" + config.ConfigurationMap.SPANNER_DB
req.Database = "projects/" + config.ConfigurationMap.GoogleProjectID + "/instances/" + k + "/databases/" + config.ConfigurationMap.SpannerDb
ddlResp, err := cli.GetDatabaseDdl(ctx, req)
if err != nil {
return nil, errors.New(err.Error())
Expand Down Expand Up @@ -808,6 +808,7 @@ func (s Storage) SpannerAdd(ctx context.Context, table string, m map[string]inte
return updatedObj, err
}

// SpannerDel for delete operation on Spanner
func (s Storage) SpannerDel(ctx context.Context, table string, m map[string]interface{}, eval *models.Eval, expr *models.UpdateExpressionCondition) error {
tableConf, err := config.GetTableConf(table)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func init() {
}
}

// Storage object for intracting with storage package
type Storage struct {
spannerClient map[string]*spanner.Client
}
Expand All @@ -60,7 +61,7 @@ var storage *Storage
func initSpannerDriver(instance string, m map[string]*gjson.Result) *spanner.Client {
conf := spanner.ClientConfig{}

str := "projects/" + config.ConfigurationMap.GOOGLE_PROJECT_ID + "/instances/" + instance + "/databases/" + config.ConfigurationMap.SPANNER_DB
str := "projects/" + config.ConfigurationMap.GoogleProjectID + "/instances/" + instance + "/databases/" + config.ConfigurationMap.SpannerDb
Client, err := spanner.NewClientWithConfig(context.Background(), str, conf)
if err != nil {
logger.LogFatal(err)
Expand Down
1 change: 1 addition & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

var base64Regexp = regexp.MustCompile("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$")

// GetFieldNameFromConditionalExpression returns the field name from conditional expression
func GetFieldNameFromConditionalExpression(conditionalExpression string) string {
if strings.Contains(conditionalExpression, "attribute_exists") {
return GetStringInBetween(conditionalExpression, "(", ")")
Expand Down

0 comments on commit fe7d852

Please sign in to comment.