github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/endpoints/scan/scan.go (about)

     1  // Support for the DynamoDB Scan endpoint.
     2  //
     3  // example use:
     4  //
     5  // tests/scan-livestest.go
     6  //
     7  package scan
     8  
     9  import (
    10  	"encoding/json"
    11  	"errors"
    12  	"github.com/smugmug/godynamo/authreq"
    13  	"github.com/smugmug/godynamo/aws_const"
    14  	"github.com/smugmug/godynamo/conf"
    15  	"github.com/smugmug/godynamo/types/attributestoget"
    16  	"github.com/smugmug/godynamo/types/attributevalue"
    17  	"github.com/smugmug/godynamo/types/aws_strings"
    18  	"github.com/smugmug/godynamo/types/capacity"
    19  	"github.com/smugmug/godynamo/types/condition"
    20  	"github.com/smugmug/godynamo/types/expressionattributenames"
    21  	"github.com/smugmug/godynamo/types/item"
    22  )
    23  
    24  const (
    25  	ENDPOINT_NAME   = "Scan"
    26  	SCAN_ENDPOINT   = aws_const.ENDPOINT_PREFIX + ENDPOINT_NAME
    27  	OP_EQ           = aws_strings.OP_EQ
    28  	OP_NE           = aws_strings.OP_NE
    29  	OP_LE           = aws_strings.OP_LE
    30  	OP_LT           = aws_strings.OP_LT
    31  	OP_GE           = aws_strings.OP_GE
    32  	OP_GT           = aws_strings.OP_GT
    33  	OP_NULL         = aws_strings.OP_NULL
    34  	OP_NOT_NULL     = aws_strings.OP_NOT_NULL
    35  	OP_CONTAINS     = aws_strings.OP_CONTAINS
    36  	OP_NOT_CONTAINS = aws_strings.OP_NOT_CONTAINS
    37  	OP_BEGINS_WITH  = aws_strings.OP_BEGINS_WITH
    38  	OP_IN           = aws_strings.OP_IN
    39  	OP_BETWEEN      = aws_strings.OP_BETWEEN
    40  	LIMIT           = 10000 // limit of scan unless set
    41  )
    42  
    43  type ComparisonOperator string
    44  
    45  type Scan struct {
    46  	AttributesToGet           attributestoget.AttributesToGet                   `json:",omitempty"`
    47  	ConditionalOperator       string                                            `json:",omitempty"`
    48  	ExclusiveStartKey         attributevalue.AttributeValueMap                  `json:",omitempty"`
    49  	ExpressionAttributeNames  expressionattributenames.ExpressionAttributeNames `json:",omitempty"`
    50  	ExpressionAttributeValues attributevalue.AttributeValueMap                  `json:",omitempty"`
    51  	FilterExpression          string                                            `json:",omitempty"`
    52  	Limit                     uint64                                            `json:",omitempty"`
    53  	ProjectionExpression      string                                            `json:",omitempty"`
    54  	ReturnConsumedCapacity    string                                            `json:",omitempty"`
    55  	ScanFilter                condition.Conditions
    56  	Segment                   uint64 `json:",omitempty"`
    57  	Select                    string `json:",omitempty"`
    58  	TableName                 string
    59  	TotalSegments             uint64 `json:",omitempty"`
    60  }
    61  
    62  func NewScan() *Scan {
    63  	s := new(Scan)
    64  	s.AttributesToGet = attributestoget.NewAttributesToGet()
    65  	s.ExclusiveStartKey = attributevalue.NewAttributeValueMap()
    66  	s.ExpressionAttributeNames = expressionattributenames.NewExpressionAttributeNames()
    67  	s.ExpressionAttributeValues = attributevalue.NewAttributeValueMap()
    68  	s.ScanFilter = condition.NewConditions()
    69  	return s
    70  }
    71  
    72  type Request Scan
    73  
    74  type Response struct {
    75  	ConsumedCapacity *capacity.ConsumedCapacity `json:",omitempty"`
    76  	Count            uint64
    77  	Items            []item.Item                      `json:",omitempty"`
    78  	LastEvaluatedKey attributevalue.AttributeValueMap `json:",omitempty"`
    79  	ScannedCount     uint64                           `json:",omitempty"`
    80  }
    81  
    82  func NewResponse() *Response {
    83  	r := new(Response)
    84  	r.ConsumedCapacity = capacity.NewConsumedCapacity()
    85  	r.Items = make([]item.Item, 0)
    86  	r.LastEvaluatedKey = attributevalue.NewAttributeValueMap()
    87  	return r
    88  }
    89  
    90  // These implementations of EndpointReq use a parameterized conf.
    91  
    92  func (scan *Scan) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
    93  	if scan == nil {
    94  		return nil, 0, errors.New("scan.(Scan)EndpointReqWithConf: receiver is nil")
    95  	}
    96  	if !conf.IsValid(c) {
    97  		return nil, 0, errors.New("scan.EndpointReqWithConf: c is not valid")
    98  	}
    99  	// returns resp_body,code,err
   100  	reqJSON, json_err := json.Marshal(scan)
   101  	if json_err != nil {
   102  		return nil, 0, json_err
   103  	}
   104  	return authreq.RetryReqJSON_V4WithConf(reqJSON, SCAN_ENDPOINT, c)
   105  }
   106  
   107  func (req *Request) EndpointWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
   108  	if req == nil {
   109  		return nil, 0, errors.New("scan.(Request)EndpointReqWithConf: receiver is nil")
   110  	}
   111  	scan := Scan(*req)
   112  	return scan.EndpointReqWithConf(c)
   113  }
   114  
   115  // These implementations of EndpointReq use the global conf.
   116  
   117  func (scan *Scan) EndpointReq() ([]byte, int, error) {
   118  	if scan == nil {
   119  		return nil, 0, errors.New("scan.(Scan)EndpointReq: receiver is nil")
   120  	}
   121  	return scan.EndpointReqWithConf(&conf.Vals)
   122  }
   123  
   124  func (req *Request) EndpointReq() ([]byte, int, error) {
   125  	if req == nil {
   126  		return nil, 0, errors.New("scan.(Request)EndpointReq: receiver is nil")
   127  	}
   128  	scan := Scan(*req)
   129  	return scan.EndpointReqWithConf(&conf.Vals)
   130  }
   131  
   132  // ValidOp determines if an operation is in the approved list.
   133  func ValidOp(op string) bool {
   134  	return (op == OP_EQ ||
   135  		op == OP_NE ||
   136  		op == OP_LE ||
   137  		op == OP_LT ||
   138  		op == OP_GE ||
   139  		op == OP_GT ||
   140  		op == OP_NULL ||
   141  		op == OP_NOT_NULL ||
   142  		op == OP_CONTAINS ||
   143  		op == OP_NOT_CONTAINS ||
   144  		op == OP_BEGINS_WITH ||
   145  		op == OP_IN ||
   146  		op == OP_BETWEEN)
   147  }