github.com/aavshr/aws-sdk-go@v1.41.3/service/dynamodb/expression/key_condition.go (about)

     1  package expression
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // keyConditionMode specifies the types of the struct KeyConditionBuilder,
     8  // representing the different types of KeyConditions (i.e. And, Or, Between, ...)
     9  type keyConditionMode int
    10  
    11  const (
    12  	// unsetKeyCond catches errors for unset KeyConditionBuilder structs
    13  	unsetKeyCond keyConditionMode = iota
    14  	// invalidKeyCond catches errors in the construction of KeyConditionBuilder structs
    15  	invalidKeyCond
    16  	// equalKeyCond represents the Equals KeyCondition
    17  	equalKeyCond
    18  	// lessThanKeyCond represents the Less Than KeyCondition
    19  	lessThanKeyCond
    20  	// lessThanEqualKeyCond represents the Less Than Or Equal To KeyCondition
    21  	lessThanEqualKeyCond
    22  	// greaterThanKeyCond represents the Greater Than KeyCondition
    23  	greaterThanKeyCond
    24  	// greaterThanEqualKeyCond represents the Greater Than Or Equal To KeyCondition
    25  	greaterThanEqualKeyCond
    26  	// andKeyCond represents the Logical And KeyCondition
    27  	andKeyCond
    28  	// betweenKeyCond represents the Between KeyCondition
    29  	betweenKeyCond
    30  	// beginsWithKeyCond represents the Begins With KeyCondition
    31  	beginsWithKeyCond
    32  )
    33  
    34  // KeyConditionBuilder represents Key Condition Expressions in DynamoDB.
    35  // KeyConditionBuilders are the building blocks of Expressions.
    36  // More Information at: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.KeyConditionExpressions
    37  type KeyConditionBuilder struct {
    38  	operandList      []OperandBuilder
    39  	keyConditionList []KeyConditionBuilder
    40  	mode             keyConditionMode
    41  }
    42  
    43  // KeyEqual returns a KeyConditionBuilder representing the equality clause
    44  // of the two argument OperandBuilders. The resulting KeyConditionBuilder can be
    45  // used as a part of other Key Condition Expressions or as an argument to the
    46  // WithKeyCondition() method for the Builder struct.
    47  //
    48  // Example:
    49  //
    50  //     // keyCondition represents the equal clause of the key "foo" and the
    51  //     // value 5
    52  //     keyCondition := expression.KeyEqual(expression.Key("foo"), expression.Value(5))
    53  //
    54  //     // Used in another Key Condition Expression
    55  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
    56  //     // Used to make an Builder
    57  //     builder := expression.NewBuilder().WithKeyCondition(keyCondition)
    58  //
    59  // Expression Equivalent:
    60  //
    61  //     expression.KeyEqual(expression.Key("foo"), expression.Value(5))
    62  //     // Let :five be an ExpressionAttributeValue representing the value 5
    63  //     "foo = :five"
    64  func KeyEqual(keyBuilder KeyBuilder, valueBuilder ValueBuilder) KeyConditionBuilder {
    65  	return KeyConditionBuilder{
    66  		operandList: []OperandBuilder{keyBuilder, valueBuilder},
    67  		mode:        equalKeyCond,
    68  	}
    69  }
    70  
    71  // Equal returns a KeyConditionBuilder representing the equality clause of
    72  // the two argument OperandBuilders. The resulting KeyConditionBuilder can be
    73  // used as a part of other Key Condition Expressions or as an argument to the
    74  // WithKeyCondition() method for the Builder struct.
    75  //
    76  // Example:
    77  //
    78  //     // keyCondition represents the equal clause of the key "foo" and the
    79  //     // value 5
    80  //     keyCondition := expression.Key("foo").Equal(expression.Value(5))
    81  //
    82  //     // Used in another Key Condition Expression
    83  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
    84  //     // Used to make an Builder
    85  //     builder := expression.NewBuilder().WithKeyCondition(keyCondition)
    86  //
    87  // Expression Equivalent:
    88  //
    89  //     expression.Key("foo").Equal(expression.Value(5))
    90  //     // Let :five be an ExpressionAttributeValue representing the value 5
    91  //     "foo = :five"
    92  func (kb KeyBuilder) Equal(valueBuilder ValueBuilder) KeyConditionBuilder {
    93  	return KeyEqual(kb, valueBuilder)
    94  }
    95  
    96  // KeyLessThan returns a KeyConditionBuilder representing the less than
    97  // clause of the two argument OperandBuilders. The resulting KeyConditionBuilder
    98  // can be used as a part of other Key Condition Expressions.
    99  //
   100  // Example:
   101  //
   102  //     // keyCondition represents the less than clause of the key "foo" and the
   103  //     // value 5
   104  //     keyCondition := expression.KeyLessThan(expression.Key("foo"), expression.Value(5))
   105  //
   106  //     // Used in another Key Condition Expression
   107  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   108  //
   109  // Expression Equivalent:
   110  //
   111  //     expression.KeyLessThan(expression.Key("foo"), expression.Value(5))
   112  //     // Let :five be an ExpressionAttributeValue representing the value 5
   113  //     "foo < :five"
   114  func KeyLessThan(keyBuilder KeyBuilder, valueBuilder ValueBuilder) KeyConditionBuilder {
   115  	return KeyConditionBuilder{
   116  		operandList: []OperandBuilder{keyBuilder, valueBuilder},
   117  		mode:        lessThanKeyCond,
   118  	}
   119  }
   120  
   121  // LessThan returns a KeyConditionBuilder representing the less than clause
   122  // of the two argument OperandBuilders. The resulting KeyConditionBuilder can be
   123  // used as a part of other Key Condition Expressions.
   124  //
   125  // Example:
   126  //
   127  //     // keyCondition represents the less than clause of the key "foo" and the
   128  //     // value 5
   129  //     keyCondition := expression.Key("foo").LessThan(expression.Value(5))
   130  //
   131  //     // Used in another Key Condition Expression
   132  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   133  //
   134  // Expression Equivalent:
   135  //
   136  //     expression.Key("foo").LessThan(expression.Value(5))
   137  //     // Let :five be an ExpressionAttributeValue representing the value 5
   138  //     "foo < :five"
   139  func (kb KeyBuilder) LessThan(valueBuilder ValueBuilder) KeyConditionBuilder {
   140  	return KeyLessThan(kb, valueBuilder)
   141  }
   142  
   143  // KeyLessThanEqual returns a KeyConditionBuilder representing the less than
   144  // equal to clause of the two argument OperandBuilders. The resulting
   145  // KeyConditionBuilder can be used as a part of other Key Condition Expressions.
   146  //
   147  // Example:
   148  //
   149  //     // keyCondition represents the less than equal to clause of the key
   150  //     // "foo" and the value 5
   151  //     keyCondition := expression.KeyLessThanEqual(expression.Key("foo"), expression.Value(5))
   152  //
   153  //     // Used in another Key Condition Expression
   154  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   155  //
   156  // Expression Equivalent:
   157  //
   158  //     expression.KeyLessThanEqual(expression.Key("foo"), expression.Value(5))
   159  //     // Let :five be an ExpressionAttributeValue representing the value 5
   160  //     "foo <= :five"
   161  func KeyLessThanEqual(keyBuilder KeyBuilder, valueBuilder ValueBuilder) KeyConditionBuilder {
   162  	return KeyConditionBuilder{
   163  		operandList: []OperandBuilder{keyBuilder, valueBuilder},
   164  		mode:        lessThanEqualKeyCond,
   165  	}
   166  }
   167  
   168  // LessThanEqual returns a KeyConditionBuilder representing the less than
   169  // equal to clause of the two argument OperandBuilders. The resulting
   170  // KeyConditionBuilder can be used as a part of other Key Condition Expressions.
   171  //
   172  // Example:
   173  //
   174  //     // keyCondition represents the less than equal to clause of the key
   175  //     // "foo" and the value 5
   176  //     keyCondition := expression.Key("foo").LessThanEqual(expression.Value(5))
   177  //
   178  //     // Used in another Key Condition Expression
   179  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   180  //
   181  // Expression Equivalent:
   182  //
   183  //     expression.Key("foo").LessThanEqual(expression.Value(5))
   184  //     // Let :five be an ExpressionAttributeValue representing the value 5
   185  //     "foo <= :five"
   186  func (kb KeyBuilder) LessThanEqual(valueBuilder ValueBuilder) KeyConditionBuilder {
   187  	return KeyLessThanEqual(kb, valueBuilder)
   188  }
   189  
   190  // KeyGreaterThan returns a KeyConditionBuilder representing the greater
   191  // than clause of the two argument OperandBuilders. The resulting
   192  // KeyConditionBuilder can be used as a part of other Key Condition Expressions.
   193  //
   194  // Example:
   195  //
   196  //     // keyCondition represents the greater than clause of the key "foo" and
   197  //     // the value 5
   198  //     keyCondition := expression.KeyGreaterThan(expression.Key("foo"), expression.Value(5))
   199  //
   200  //     // Used in another Key Condition Expression
   201  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   202  //
   203  // Expression Equivalent:
   204  //
   205  //     expression.KeyGreaterThan(expression.Key("foo"), expression.Value(5))
   206  //     // Let :five be an ExpressionAttributeValue representing the value 5
   207  //     "foo > :five"
   208  func KeyGreaterThan(keyBuilder KeyBuilder, valueBuilder ValueBuilder) KeyConditionBuilder {
   209  	return KeyConditionBuilder{
   210  		operandList: []OperandBuilder{keyBuilder, valueBuilder},
   211  		mode:        greaterThanKeyCond,
   212  	}
   213  }
   214  
   215  // GreaterThan returns a KeyConditionBuilder representing the greater than
   216  // clause of the two argument OperandBuilders. The resulting KeyConditionBuilder
   217  // can be used as a part of other Key Condition Expressions.
   218  //
   219  // Example:
   220  //
   221  //     // key condition represents the greater than clause of the key "foo" and
   222  //     // the value 5
   223  //     keyCondition := expression.Key("foo").GreaterThan(expression.Value(5))
   224  //
   225  //     // Used in another Key Condition Expression
   226  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   227  //
   228  // Expression Equivalent:
   229  //
   230  //     expression.Key("foo").GreaterThan(expression.Value(5))
   231  //     // Let :five be an ExpressionAttributeValue representing the value 5
   232  //     "foo > :five"
   233  func (kb KeyBuilder) GreaterThan(valueBuilder ValueBuilder) KeyConditionBuilder {
   234  	return KeyGreaterThan(kb, valueBuilder)
   235  }
   236  
   237  // KeyGreaterThanEqual returns a KeyConditionBuilder representing the
   238  // greater than equal to clause of the two argument OperandBuilders. The
   239  // resulting KeyConditionBuilder can be used as a part of other Key Condition
   240  // Expressions.
   241  //
   242  // Example:
   243  //
   244  //     // keyCondition represents the greater than equal to clause of the key
   245  //     // "foo" and the value 5
   246  //     keyCondition := expression.KeyGreaterThanEqual(expression.Key("foo"), expression.Value(5))
   247  //
   248  //     // Used in another Key Condition Expression
   249  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   250  //
   251  // Expression Equivalent:
   252  //
   253  //     expression.KeyGreaterThanEqual(expression.Key("foo"), expression.Value(5))
   254  //     // Let :five be an ExpressionAttributeValue representing the value 5
   255  //     "foo >= :five"
   256  func KeyGreaterThanEqual(keyBuilder KeyBuilder, valueBuilder ValueBuilder) KeyConditionBuilder {
   257  	return KeyConditionBuilder{
   258  		operandList: []OperandBuilder{keyBuilder, valueBuilder},
   259  		mode:        greaterThanEqualKeyCond,
   260  	}
   261  }
   262  
   263  // GreaterThanEqual returns a KeyConditionBuilder representing the greater
   264  // than equal to clause of the two argument OperandBuilders. The resulting
   265  // KeyConditionBuilder can be used as a part of other Key Condition Expressions.
   266  //
   267  // Example:
   268  //
   269  //     // keyCondition represents the greater than equal to clause of the key
   270  //     // "foo" and the value 5
   271  //     keyCondition := expression.Key("foo").GreaterThanEqual(expression.Value(5))
   272  //
   273  //     // Used in another Key Condition Expression
   274  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   275  //
   276  // Expression Equivalent:
   277  //
   278  //     expression.Key("foo").GreaterThanEqual(expression.Value(5))
   279  //     // Let :five be an ExpressionAttributeValue representing the value 5
   280  //     "foo >= :five"
   281  func (kb KeyBuilder) GreaterThanEqual(valueBuilder ValueBuilder) KeyConditionBuilder {
   282  	return KeyGreaterThanEqual(kb, valueBuilder)
   283  }
   284  
   285  // KeyAnd returns a KeyConditionBuilder representing the logical AND clause
   286  // of the two argument KeyConditionBuilders. The resulting KeyConditionBuilder
   287  // can be used as an argument to the WithKeyCondition() method for the Builder
   288  // struct.
   289  //
   290  // Example:
   291  //
   292  //     // keyCondition represents the key condition where the partition key
   293  //     // "TeamName" is equal to value "Wildcats" and sort key "Number" is equal
   294  //     // to value 1
   295  //     keyCondition := expression.KeyAnd(expression.Key("TeamName").Equal(expression.Value("Wildcats")), expression.Key("Number").Equal(expression.Value(1)))
   296  //
   297  //     // Used to make an Builder
   298  //     builder := expression.NewBuilder().WithKeyCondition(keyCondition)
   299  //
   300  // Expression Equivalent:
   301  //
   302  //     expression.KeyAnd(expression.Key("TeamName").Equal(expression.Value("Wildcats")), expression.Key("Number").Equal(expression.Value(1)))
   303  //     // Let #NUMBER, :teamName, and :one be ExpressionAttributeName and
   304  //     // ExpressionAttributeValues representing the item attribute "Number",
   305  //     // the value "Wildcats", and the value 1
   306  //     "(TeamName = :teamName) AND (#NUMBER = :one)"
   307  func KeyAnd(left, right KeyConditionBuilder) KeyConditionBuilder {
   308  	if left.mode != equalKeyCond {
   309  		return KeyConditionBuilder{
   310  			mode: invalidKeyCond,
   311  		}
   312  	}
   313  	if right.mode == andKeyCond {
   314  		return KeyConditionBuilder{
   315  			mode: invalidKeyCond,
   316  		}
   317  	}
   318  	return KeyConditionBuilder{
   319  		keyConditionList: []KeyConditionBuilder{left, right},
   320  		mode:             andKeyCond,
   321  	}
   322  }
   323  
   324  // And returns a KeyConditionBuilder representing the logical AND clause of
   325  // the two argument KeyConditionBuilders. The resulting KeyConditionBuilder can
   326  // be used as an argument to the WithKeyCondition() method for the Builder
   327  // struct.
   328  //
   329  // Example:
   330  //
   331  //     // keyCondition represents the key condition where the partition key
   332  //     // "TeamName" is equal to value "Wildcats" and sort key "Number" is equal
   333  //     // to value 1
   334  //     keyCondition := expression.Key("TeamName").Equal(expression.Value("Wildcats")).And(expression.Key("Number").Equal(expression.Value(1)))
   335  //
   336  //     // Used to make an Builder
   337  //     builder := expression.NewBuilder().WithKeyCondition(keyCondition)
   338  //
   339  // Expression Equivalent:
   340  //
   341  //     expression.Key("TeamName").Equal(expression.Value("Wildcats")).And(expression.Key("Number").Equal(expression.Value(1)))
   342  //     // Let #NUMBER, :teamName, and :one be ExpressionAttributeName and
   343  //     // ExpressionAttributeValues representing the item attribute "Number",
   344  //     // the value "Wildcats", and the value 1
   345  //     "(TeamName = :teamName) AND (#NUMBER = :one)"
   346  func (kcb KeyConditionBuilder) And(right KeyConditionBuilder) KeyConditionBuilder {
   347  	return KeyAnd(kcb, right)
   348  }
   349  
   350  // KeyBetween returns a KeyConditionBuilder representing the result of the
   351  // BETWEEN function in DynamoDB Key Condition Expressions. The resulting
   352  // KeyConditionBuilder can be used as a part of other Key Condition Expressions.
   353  //
   354  // Example:
   355  //
   356  //     // keyCondition represents the boolean key condition of whether the value
   357  //     // of the key "foo" is between values 5 and 10
   358  //     keyCondition := expression.KeyBetween(expression.Key("foo"), expression.Value(5), expression.Value(10))
   359  //
   360  //     // Used in another Key Condition Expression
   361  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   362  //
   363  // Expression Equivalent:
   364  //
   365  //     expression.KeyBetween(expression.Key("foo"), expression.Value(5), expression.Value(10))
   366  //     // Let :five and :ten be ExpressionAttributeValues representing the
   367  //     // values 5 and 10 respectively
   368  //     "foo BETWEEN :five AND :ten"
   369  func KeyBetween(keyBuilder KeyBuilder, lower, upper ValueBuilder) KeyConditionBuilder {
   370  	return KeyConditionBuilder{
   371  		operandList: []OperandBuilder{keyBuilder, lower, upper},
   372  		mode:        betweenKeyCond,
   373  	}
   374  }
   375  
   376  // Between returns a KeyConditionBuilder representing the result of the
   377  // BETWEEN function in DynamoDB Key Condition Expressions. The resulting
   378  // KeyConditionBuilder can be used as a part of other Key Condition Expressions.
   379  //
   380  // Example:
   381  //
   382  //     // keyCondition represents the boolean key condition of whether the value
   383  //     // of the key "foo" is between values 5 and 10
   384  //     keyCondition := expression.Key("foo").Between(expression.Value(5), expression.Value(10))
   385  //
   386  //     // Used in another Key Condition Expression
   387  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   388  //
   389  // Expression Equivalent:
   390  //
   391  //     expression.Key("foo").Between(expression.Value(5), expression.Value(10))
   392  //     // Let :five and :ten be ExpressionAttributeValues representing the
   393  //     // values 5 and 10 respectively
   394  //     "foo BETWEEN :five AND :ten"
   395  func (kb KeyBuilder) Between(lower, upper ValueBuilder) KeyConditionBuilder {
   396  	return KeyBetween(kb, lower, upper)
   397  }
   398  
   399  // KeyBeginsWith returns a KeyConditionBuilder representing the result of
   400  // the begins_with function in DynamoDB Key Condition Expressions. The resulting
   401  // KeyConditionBuilder can be used as a part of other Key Condition Expressions.
   402  //
   403  // Example:
   404  //
   405  //     // keyCondition represents the boolean key condition of whether the value
   406  //     // of the key "foo" is begins with the prefix "bar"
   407  //     keyCondition := expression.KeyBeginsWith(expression.Key("foo"), "bar")
   408  //
   409  //     // Used in another Key Condition Expression
   410  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   411  //
   412  // Expression Equivalent:
   413  //
   414  //     expression.KeyBeginsWith(expression.Key("foo"), "bar")
   415  //     // Let :bar be an ExpressionAttributeValue representing the value "bar"
   416  //     "begins_with(foo, :bar)"
   417  func KeyBeginsWith(keyBuilder KeyBuilder, prefix string) KeyConditionBuilder {
   418  	valueBuilder := ValueBuilder{
   419  		value: prefix,
   420  	}
   421  	return KeyConditionBuilder{
   422  		operandList: []OperandBuilder{keyBuilder, valueBuilder},
   423  		mode:        beginsWithKeyCond,
   424  	}
   425  }
   426  
   427  // BeginsWith returns a KeyConditionBuilder representing the result of the
   428  // begins_with function in DynamoDB Key Condition Expressions. The resulting
   429  // KeyConditionBuilder can be used as a part of other Key Condition Expressions.
   430  //
   431  // Example:
   432  //
   433  //     // keyCondition represents the boolean key condition of whether the value
   434  //     // of the key "foo" is begins with the prefix "bar"
   435  //     keyCondition := expression.Key("foo").BeginsWith("bar")
   436  //
   437  //     // Used in another Key Condition Expression
   438  //     anotherKeyCondition := expression.Key("partitionKey").Equal(expression.Value("aValue")).And(keyCondition)
   439  //
   440  // Expression Equivalent:
   441  //
   442  //     expression.Key("foo").BeginsWith("bar")
   443  //     // Let :bar be an ExpressionAttributeValue representing the value "bar"
   444  //     "begins_with(foo, :bar)"
   445  func (kb KeyBuilder) BeginsWith(prefix string) KeyConditionBuilder {
   446  	return KeyBeginsWith(kb, prefix)
   447  }
   448  
   449  // buildTree builds a tree structure of exprNodes based on the tree
   450  // structure of the input KeyConditionBuilder's child KeyConditions/Operands.
   451  // buildTree() satisfies the treeBuilder interface so KeyConditionBuilder can be
   452  // a part of Expression struct.
   453  func (kcb KeyConditionBuilder) buildTree() (exprNode, error) {
   454  	childNodes, err := kcb.buildChildNodes()
   455  	if err != nil {
   456  		return exprNode{}, err
   457  	}
   458  	ret := exprNode{
   459  		children: childNodes,
   460  	}
   461  
   462  	switch kcb.mode {
   463  	case equalKeyCond, lessThanKeyCond, lessThanEqualKeyCond, greaterThanKeyCond, greaterThanEqualKeyCond:
   464  		return compareBuildKeyCondition(kcb.mode, ret)
   465  	case andKeyCond:
   466  		return andBuildKeyCondition(kcb, ret)
   467  	case betweenKeyCond:
   468  		return betweenBuildKeyCondition(ret)
   469  	case beginsWithKeyCond:
   470  		return beginsWithBuildKeyCondition(ret)
   471  	case unsetKeyCond:
   472  		return exprNode{}, newUnsetParameterError("buildTree", "KeyConditionBuilder")
   473  	case invalidKeyCond:
   474  		return exprNode{}, fmt.Errorf("buildKeyCondition error: invalid key condition constructed")
   475  	default:
   476  		return exprNode{}, fmt.Errorf("buildKeyCondition error: unsupported mode: %v", kcb.mode)
   477  	}
   478  }
   479  
   480  // compareBuildKeyCondition is the function to make exprNodes from Compare
   481  // KeyConditionBuilders. compareBuildKeyCondition is only called by the
   482  // buildKeyCondition method. This function assumes that the argument
   483  // KeyConditionBuilder has the right format.
   484  func compareBuildKeyCondition(keyConditionMode keyConditionMode, node exprNode) (exprNode, error) {
   485  	// Create a string with special characters that can be substituted later: $c
   486  	switch keyConditionMode {
   487  	case equalKeyCond:
   488  		node.fmtExpr = "$c = $c"
   489  	case lessThanKeyCond:
   490  		node.fmtExpr = "$c < $c"
   491  	case lessThanEqualKeyCond:
   492  		node.fmtExpr = "$c <= $c"
   493  	case greaterThanKeyCond:
   494  		node.fmtExpr = "$c > $c"
   495  	case greaterThanEqualKeyCond:
   496  		node.fmtExpr = "$c >= $c"
   497  	default:
   498  		return exprNode{}, fmt.Errorf("build compare key condition error: unsupported mode: %v", keyConditionMode)
   499  	}
   500  
   501  	return node, nil
   502  }
   503  
   504  // andBuildKeyCondition is the function to make exprNodes from And
   505  // KeyConditionBuilders. andBuildKeyCondition is only called by the
   506  // buildKeyCondition method. This function assumes that the argument
   507  // KeyConditionBuilder has the right format.
   508  func andBuildKeyCondition(keyConditionBuilder KeyConditionBuilder, node exprNode) (exprNode, error) {
   509  	if len(keyConditionBuilder.keyConditionList) == 0 && len(keyConditionBuilder.operandList) == 0 {
   510  		return exprNode{}, newInvalidParameterError("andBuildKeyCondition", "KeyConditionBuilder")
   511  	}
   512  	// create a string with escaped characters to substitute them with proper
   513  	// aliases during runtime
   514  	node.fmtExpr = "($c) AND ($c)"
   515  
   516  	return node, nil
   517  }
   518  
   519  // betweenBuildKeyCondition is the function to make exprNodes from Between
   520  // KeyConditionBuilders. betweenBuildKeyCondition is only called by the
   521  // buildKeyCondition method. This function assumes that the argument
   522  // KeyConditionBuilder has the right format.
   523  func betweenBuildKeyCondition(node exprNode) (exprNode, error) {
   524  	// Create a string with special characters that can be substituted later: $c
   525  	node.fmtExpr = "$c BETWEEN $c AND $c"
   526  
   527  	return node, nil
   528  }
   529  
   530  // beginsWithBuildKeyCondition is the function to make exprNodes from
   531  // BeginsWith KeyConditionBuilders. beginsWithBuildKeyCondition is only
   532  // called by the buildKeyCondition method. This function assumes that the argument
   533  // KeyConditionBuilder has the right format.
   534  func beginsWithBuildKeyCondition(node exprNode) (exprNode, error) {
   535  	// Create a string with special characters that can be substituted later: $c
   536  	node.fmtExpr = "begins_with ($c, $c)"
   537  
   538  	return node, nil
   539  }
   540  
   541  // buildChildNodes creates the list of the child exprNodes. This avoids
   542  // duplication of code amongst the various buildConditions.
   543  func (kcb KeyConditionBuilder) buildChildNodes() ([]exprNode, error) {
   544  	childNodes := make([]exprNode, 0, len(kcb.keyConditionList)+len(kcb.operandList))
   545  	for _, keyCondition := range kcb.keyConditionList {
   546  		node, err := keyCondition.buildTree()
   547  		if err != nil {
   548  			return []exprNode{}, err
   549  		}
   550  		childNodes = append(childNodes, node)
   551  	}
   552  	for _, operand := range kcb.operandList {
   553  		ope, err := operand.BuildOperand()
   554  		if err != nil {
   555  			return []exprNode{}, err
   556  		}
   557  		childNodes = append(childNodes, ope.exprNode)
   558  	}
   559  
   560  	return childNodes, nil
   561  }