condition.go 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  1. package expression
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // conditionMode specifies the types of the struct conditionBuilder,
  7. // representing the different types of Conditions (i.e. And, Or, Between, ...)
  8. type conditionMode int
  9. const (
  10. // unsetCond catches errors for unset ConditionBuilder structs
  11. unsetCond conditionMode = iota
  12. // equalCond represents the Equals Condition
  13. equalCond
  14. // notEqualCond represents the Not Equals Condition
  15. notEqualCond
  16. // lessThanCond represents the LessThan Condition
  17. lessThanCond
  18. // lessThanEqualCond represents the LessThanOrEqual Condition
  19. lessThanEqualCond
  20. // greaterThanCond represents the GreaterThan Condition
  21. greaterThanCond
  22. // greaterThanEqualCond represents the GreaterThanEqual Condition
  23. greaterThanEqualCond
  24. // andCond represents the Logical And Condition
  25. andCond
  26. // orCond represents the Logical Or Condition
  27. orCond
  28. // notCond represents the Logical Not Condition
  29. notCond
  30. // betweenCond represents the Between Condition
  31. betweenCond
  32. // inCond represents the In Condition
  33. inCond
  34. // attrExistsCond represents the Attribute Exists Condition
  35. attrExistsCond
  36. // attrNotExistsCond represents the Attribute Not Exists Condition
  37. attrNotExistsCond
  38. // attrTypeCond represents the Attribute Type Condition
  39. attrTypeCond
  40. // beginsWithCond represents the Begins With Condition
  41. beginsWithCond
  42. // containsCond represents the Contains Condition
  43. containsCond
  44. )
  45. // DynamoDBAttributeType specifies the type of an DynamoDB item attribute. This
  46. // enum is used in the AttributeType() function in order to be explicit about
  47. // the DynamoDB type that is being checked and ensure compile time checks.
  48. // More Informatin at http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Functions
  49. type DynamoDBAttributeType string
  50. const (
  51. // String represents the DynamoDB String type
  52. String DynamoDBAttributeType = "S"
  53. // StringSet represents the DynamoDB String Set type
  54. StringSet = "SS"
  55. // Number represents the DynamoDB Number type
  56. Number = "N"
  57. // NumberSet represents the DynamoDB Number Set type
  58. NumberSet = "NS"
  59. // Binary represents the DynamoDB Binary type
  60. Binary = "B"
  61. // BinarySet represents the DynamoDB Binary Set type
  62. BinarySet = "BS"
  63. // Boolean represents the DynamoDB Boolean type
  64. Boolean = "BOOL"
  65. // Null represents the DynamoDB Null type
  66. Null = "NULL"
  67. // List represents the DynamoDB List type
  68. List = "L"
  69. // Map represents the DynamoDB Map type
  70. Map = "M"
  71. )
  72. // ConditionBuilder represents Condition Expressions and Filter Expressions
  73. // in DynamoDB. ConditionBuilders are one of the building blocks of the Builder
  74. // struct. Since Filter Expressions support all the same functions and formats
  75. // as Condition Expressions, ConditionBuilders represents both types of
  76. // Expressions.
  77. // More Information at: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html
  78. // More Information on Filter Expressions: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.FilterExpression
  79. type ConditionBuilder struct {
  80. operandList []OperandBuilder
  81. conditionList []ConditionBuilder
  82. mode conditionMode
  83. }
  84. // Equal returns a ConditionBuilder representing the equality clause of the two
  85. // argument OperandBuilders. The resulting ConditionBuilder can be used as a
  86. // part of other Condition Expressions or as an argument to the WithCondition()
  87. // method for the Builder struct.
  88. //
  89. // Example:
  90. //
  91. // // condition represents the equal clause of the item attribute "foo" and
  92. // // the value 5
  93. // condition := expression.Equal(expression.Name("foo"), expression.Value(5))
  94. //
  95. // // Used in another Condition Expression
  96. // anotherCondition := expression.Not(condition)
  97. // // Used to make an Builder
  98. // builder := expression.NewBuilder().WithCondition(condition)
  99. //
  100. // Expression Equivalent:
  101. //
  102. // expression.Equal(expression.Name("foo"), expression.Value(5))
  103. // // Let :five be an ExpressionAttributeValue representing the value 5
  104. // "foo = :five"
  105. func Equal(left, right OperandBuilder) ConditionBuilder {
  106. return ConditionBuilder{
  107. operandList: []OperandBuilder{left, right},
  108. mode: equalCond,
  109. }
  110. }
  111. // Equal returns a ConditionBuilder representing the equality clause of the two
  112. // argument OperandBuilders. The resulting ConditionBuilder can be used as a
  113. // part of other Condition Expressions or as an argument to the WithCondition()
  114. // method for the Builder struct.
  115. //
  116. // Example:
  117. //
  118. // // condition represents the equal clause of the item attribute "foo" and
  119. // // the value 5
  120. // condition := expression.Name("foo").Equal(expression.Value(5))
  121. //
  122. // // Used in another Condition Expression
  123. // anotherCondition := expression.Not(condition)
  124. // // Used to make an Builder
  125. // builder := expression.NewBuilder().WithCondition(condition)
  126. //
  127. // Expression Equivalent:
  128. //
  129. // expression.Name("foo").Equal(expression.Value(5))
  130. // // Let :five be an ExpressionAttributeValue representing the value 5
  131. // "foo = :five"
  132. func (nb NameBuilder) Equal(right OperandBuilder) ConditionBuilder {
  133. return Equal(nb, right)
  134. }
  135. // Equal returns a ConditionBuilder representing the equality clause of the two
  136. // argument OperandBuilders. The resulting ConditionBuilder can be used as a
  137. // part of other Condition Expressions or as an argument to the WithCondition()
  138. // method for the Builder struct.
  139. //
  140. // Example:
  141. //
  142. // // condition represents the equal clause of the item attribute "foo" and
  143. // // the value 5
  144. // condition := expression.Value(5).Equal(expression.Name("foo"))
  145. //
  146. // // Used in another Condition Expression
  147. // anotherCondition := expression.Not(condition)
  148. // // Used to make an Builder
  149. // builder := expression.NewBuilder().WithCondition(condition)
  150. //
  151. // Expression Equivalent:
  152. //
  153. // expression.Value(5).Equal(expression.Name("foo"))
  154. // // Let :five be an ExpressionAttributeValue representing the value 5
  155. // ":five = foo"
  156. func (vb ValueBuilder) Equal(right OperandBuilder) ConditionBuilder {
  157. return Equal(vb, right)
  158. }
  159. // Equal returns a ConditionBuilder representing the equality clause of the two
  160. // argument OperandBuilders. The resulting ConditionBuilder can be used as a
  161. // part of other Condition Expressions or as an argument to the WithCondition()
  162. // method for the Builder struct.
  163. //
  164. // Example:
  165. //
  166. // // condition represents the equal clause of the size of the item
  167. // // attribute "foo" and the value 5
  168. // condition := expression.Size(expression.Name("foo")).Equal(expression.Value(5))
  169. //
  170. // // Used in another Condition Expression
  171. // anotherCondition := expression.Not(condition)
  172. // // Used to make an Builder
  173. // builder := expression.NewBuilder().WithCondition(condition)
  174. //
  175. // Expression Equivalent:
  176. //
  177. // expression.Size(expression.Name("foo")).Equal(expression.Value(5))
  178. // // Let :five be an ExpressionAttributeValue representing the value 5
  179. // "size (foo) = :five"
  180. func (sb SizeBuilder) Equal(right OperandBuilder) ConditionBuilder {
  181. return Equal(sb, right)
  182. }
  183. // NotEqual returns a ConditionBuilder representing the not equal clause of the
  184. // two argument OperandBuilders. The resulting ConditionBuilder can be used as a
  185. // part of other Condition Expressions or as an argument to the WithCondition()
  186. // method for the Builder struct.
  187. //
  188. // Example:
  189. //
  190. // // condition represents the not equal clause of the item attribute "foo"
  191. // // and the value 5
  192. // condition := expression.NotEqual(expression.Name("foo"), expression.Value(5))
  193. //
  194. // // Used in another Condition Expression
  195. // anotherCondition := expression.Not(condition)
  196. // // Used to make an Builder
  197. // builder := expression.NewBuilder().WithCondition(condition)
  198. //
  199. // Expression Equivalent:
  200. //
  201. // expression.NotEqual(expression.Name("foo"), expression.Value(5))
  202. // // Let :five be an ExpressionAttributeValue representing the value 5
  203. // "foo <> :five"
  204. func NotEqual(left, right OperandBuilder) ConditionBuilder {
  205. return ConditionBuilder{
  206. operandList: []OperandBuilder{left, right},
  207. mode: notEqualCond,
  208. }
  209. }
  210. // NotEqual returns a ConditionBuilder representing the not equal clause of the
  211. // two argument OperandBuilders. The resulting ConditionBuilder can be used as a
  212. // part of other Condition Expressions or as an argument to the WithCondition()
  213. // method for the Builder struct.
  214. //
  215. // Example:
  216. //
  217. // // condition represents the not equal clause of the item attribute "foo"
  218. // // and the value 5
  219. // condition := expression.Name("foo").NotEqual(expression.Value(5))
  220. //
  221. // // Used in another Condition Expression
  222. // anotherCondition := expression.Not(condition)
  223. // // Used to make an Builder
  224. // builder := expression.NewBuilder().WithCondition(condition)
  225. //
  226. // Expression Equivalent:
  227. //
  228. // expression.Name("foo").NotEqual(expression.Value(5))
  229. // // Let :five be an ExpressionAttributeValue representing the value 5
  230. // "foo <> :five"
  231. func (nb NameBuilder) NotEqual(right OperandBuilder) ConditionBuilder {
  232. return NotEqual(nb, right)
  233. }
  234. // NotEqual returns a ConditionBuilder representing the not equal clause of the
  235. // two argument OperandBuilders. The resulting ConditionBuilder can be used as a
  236. // part of other Condition Expressions or as an argument to the WithCondition()
  237. // method for the Builder struct.
  238. //
  239. // Example:
  240. //
  241. // // condition represents the not equal clause of the item attribute "foo"
  242. // // and the value 5
  243. // condition := expression.Value(5).NotEqual(expression.Name("foo"))
  244. //
  245. // // Used in another Condition Expression
  246. // anotherCondition := expression.Not(condition)
  247. // // Used to make an Builder
  248. // builder := expression.NewBuilder().WithCondition(condition)
  249. //
  250. // Expression Equivalent:
  251. //
  252. // expression.Value(5).NotEqual(expression.Name("foo"))
  253. // // Let :five be an ExpressionAttributeValue representing the value 5
  254. // ":five <> foo"
  255. func (vb ValueBuilder) NotEqual(right OperandBuilder) ConditionBuilder {
  256. return NotEqual(vb, right)
  257. }
  258. // NotEqual returns a ConditionBuilder representing the not equal clause of the
  259. // two argument OperandBuilders. The resulting ConditionBuilder can be used as a
  260. // part of other Condition Expressions or as an argument to the WithCondition()
  261. // method for the Builder struct.
  262. //
  263. // Example:
  264. //
  265. // // condition represents the not equal clause of the size of the item
  266. // // attribute "foo" and the value 5
  267. // condition := expression.Size(expression.Name("foo")).NotEqual(expression.Value(5))
  268. //
  269. // // Used in another Condition Expression
  270. // anotherCondition := expression.Not(condition)
  271. // // Used to make an Builder
  272. // builder := expression.NewBuilder().WithCondition(condition)
  273. //
  274. // Expression Equivalent:
  275. //
  276. // expression.Size(expression.Name("foo")).NotEqual(expression.Value(5))
  277. // // Let :five be an ExpressionAttributeValue representing the value 5
  278. // "size (foo) <> :five"
  279. func (sb SizeBuilder) NotEqual(right OperandBuilder) ConditionBuilder {
  280. return NotEqual(sb, right)
  281. }
  282. // LessThan returns a ConditionBuilder representing the less than clause of the
  283. // two argument OperandBuilders. The resulting ConditionBuilder can be used as a
  284. // part of other Condition Expressions or as an argument to the WithCondition()
  285. // method for the Builder struct.
  286. //
  287. // Example:
  288. //
  289. // // condition represents the less than clause of the item attribute "foo"
  290. // // and the value 5
  291. // condition := expression.LessThan(expression.Name("foo"), expression.Value(5))
  292. //
  293. // // Used in another Condition Expression
  294. // anotherCondition := expression.Not(condition)
  295. // // Used to make an Builder
  296. // builder := expression.NewBuilder().WithCondition(condition)
  297. //
  298. // Expression Equivalent:
  299. //
  300. // expression.LessThan(expression.Name("foo"), expression.Value(5))
  301. // // Let :five be an ExpressionAttributeValue representing the value 5
  302. // "foo < :five"
  303. func LessThan(left, right OperandBuilder) ConditionBuilder {
  304. return ConditionBuilder{
  305. operandList: []OperandBuilder{left, right},
  306. mode: lessThanCond,
  307. }
  308. }
  309. // LessThan returns a ConditionBuilder representing the less than clause of the
  310. // two argument OperandBuilders. The resulting ConditionBuilder can be used as a
  311. // part of other Condition Expressions or as an argument to the WithCondition()
  312. // method for the Builder struct.
  313. //
  314. // Example:
  315. //
  316. // // condition represents the less than clause of the item attribute "foo"
  317. // // and the value 5
  318. // condition := expression.Name("foo").LessThan(expression.Value(5))
  319. //
  320. // // Used in another Condition Expression
  321. // anotherCondition := expression.Not(condition)
  322. // // Used to make an Builder
  323. // builder := expression.NewBuilder().WithCondition(condition)
  324. //
  325. // Expression Equivalent:
  326. //
  327. // expression.Name("foo").LessThan(expression.Value(5))
  328. // // Let :five be an ExpressionAttributeValue representing the value 5
  329. // "foo < :five"
  330. func (nb NameBuilder) LessThan(right OperandBuilder) ConditionBuilder {
  331. return LessThan(nb, right)
  332. }
  333. // LessThan returns a ConditionBuilder representing the less than clause of the
  334. // two argument OperandBuilders. The resulting ConditionBuilder can be used as a
  335. // part of other Condition Expressions or as an argument to the WithCondition()
  336. // method for the Builder struct.
  337. //
  338. // Example:
  339. //
  340. // // condition represents the less than clause of the item attribute "foo"
  341. // // and the value 5
  342. // condition := expression.Value(5).LessThan(expression.Name("foo"))
  343. //
  344. // // Used in another Condition Expression
  345. // anotherCondition := expression.Not(condition)
  346. // // Used to make an Builder
  347. // builder := expression.NewBuilder().WithCondition(condition)
  348. //
  349. // Expression Equivalent:
  350. //
  351. // expression.Value(5).LessThan(expression.Name("foo"))
  352. // // Let :five be an ExpressionAttributeValue representing the value 5
  353. // ":five < foo"
  354. func (vb ValueBuilder) LessThan(right OperandBuilder) ConditionBuilder {
  355. return LessThan(vb, right)
  356. }
  357. // LessThan returns a ConditionBuilder representing the less than clause of the
  358. // two argument OperandBuilders. The resulting ConditionBuilder can be used as a
  359. // part of other Condition Expressions or as an argument to the WithCondition()
  360. // method for the Builder struct.
  361. //
  362. // Example:
  363. //
  364. // // condition represents the less than clause of the size of the item
  365. // // attribute "foo" and the value 5
  366. // condition := expression.Size(expression.Name("foo")).LessThan(expression.Value(5))
  367. //
  368. // // Used in another Condition Expression
  369. // anotherCondition := expression.Not(condition)
  370. // // Used to make an Builder
  371. // builder := expression.NewBuilder().WithCondition(condition)
  372. //
  373. // Expression Equivalent:
  374. //
  375. // expression.Size(expression.Name("foo")).LessThan(expression.Value(5))
  376. // // Let :five be an ExpressionAttributeValue representing the value 5
  377. // "size (foo) < :five"
  378. func (sb SizeBuilder) LessThan(right OperandBuilder) ConditionBuilder {
  379. return LessThan(sb, right)
  380. }
  381. // LessThanEqual returns a ConditionBuilder representing the less than equal to
  382. // clause of the two argument OperandBuilders. The resulting ConditionBuilder
  383. // can be used as a part of other Condition Expressions or as an argument to the
  384. // WithCondition() method for the Builder struct.
  385. //
  386. // Example:
  387. //
  388. // // condition represents the less than equal to clause of the item
  389. // // attribute "foo" and the value 5
  390. // condition := expression.LessThanEqual(expression.Name("foo"), expression.Value(5))
  391. //
  392. // // Used in another Condition Expression
  393. // anotherCondition := expression.Not(condition)
  394. // // Used to make an Builder
  395. // builder := expression.NewBuilder().WithCondition(condition)
  396. //
  397. // Expression Equivalent:
  398. //
  399. // expression.LessThanEqual(expression.Name("foo"), expression.Value(5))
  400. // // Let :five be an ExpressionAttributeValue representing the value 5
  401. // "foo <= :five"
  402. func LessThanEqual(left, right OperandBuilder) ConditionBuilder {
  403. return ConditionBuilder{
  404. operandList: []OperandBuilder{left, right},
  405. mode: lessThanEqualCond,
  406. }
  407. }
  408. // LessThanEqual returns a ConditionBuilder representing the less than equal to
  409. // clause of the two argument OperandBuilders. The resulting ConditionBuilder
  410. // can be used as a part of other Condition Expressions or as an argument to the
  411. // WithCondition() method for the Builder struct.
  412. //
  413. // Example:
  414. //
  415. // // condition represents the less than equal to clause of the item
  416. // // attribute "foo" and the value 5
  417. // condition := expression.Name("foo").LessThanEqual(expression.Value(5))
  418. //
  419. // // Used in another Condition Expression
  420. // anotherCondition := expression.Not(condition)
  421. // // Used to make an Builder
  422. // builder := expression.NewBuilder().WithCondition(condition)
  423. //
  424. // Expression Equivalent:
  425. //
  426. // expression.Name("foo").LessThanEqual(expression.Value(5))
  427. // // Let :five be an ExpressionAttributeValue representing the value 5
  428. // "foo <= :five"
  429. func (nb NameBuilder) LessThanEqual(right OperandBuilder) ConditionBuilder {
  430. return LessThanEqual(nb, right)
  431. }
  432. // LessThanEqual returns a ConditionBuilder representing the less than equal to
  433. // clause of the two argument OperandBuilders. The resulting ConditionBuilder
  434. // can be used as a part of other Condition Expressions or as an argument to the
  435. // WithCondition() method for the Builder struct.
  436. //
  437. // Example:
  438. //
  439. // // condition represents the less than equal to clause of the item
  440. // // attribute "foo" and the value 5
  441. // condition := expression.Value(5).LessThanEqual(expression.Name("foo"))
  442. //
  443. // // Used in another Condition Expression
  444. // anotherCondition := expression.Not(condition)
  445. // // Used to make an Builder
  446. // builder := expression.NewBuilder().WithCondition(condition)
  447. //
  448. // Expression Equivalent:
  449. //
  450. // expression.Value(5).LessThanEqual(expression.Name("foo"))
  451. // // Let :five be an ExpressionAttributeValue representing the value 5
  452. // ":five <= foo"
  453. func (vb ValueBuilder) LessThanEqual(right OperandBuilder) ConditionBuilder {
  454. return LessThanEqual(vb, right)
  455. }
  456. // LessThanEqual returns a ConditionBuilder representing the less than equal to
  457. // clause of the two argument OperandBuilders. The resulting ConditionBuilder
  458. // can be used as a part of other Condition Expressions or as an argument to the
  459. // WithCondition() method for the Builder struct.
  460. //
  461. // Example:
  462. //
  463. // // condition represents the less than equal to clause of the size of the
  464. // // item attribute "foo" and the value 5
  465. // condition := expression.Size(expression.Name("foo")).LessThanEqual(expression.Value(5))
  466. //
  467. // // Used in another Condition Expression
  468. // anotherCondition := expression.Not(condition)
  469. // // Used to make an Builder
  470. // builder := expression.NewBuilder().WithCondition(condition)
  471. //
  472. // Expression Equivalent:
  473. //
  474. // expression.Size(expression.Name("foo")).LessThanEqual(expression.Value(5))
  475. // // Let :five be an ExpressionAttributeValue representing the value 5
  476. // "size (foo) <= :five"
  477. func (sb SizeBuilder) LessThanEqual(right OperandBuilder) ConditionBuilder {
  478. return LessThanEqual(sb, right)
  479. }
  480. // GreaterThan returns a ConditionBuilder representing the greater than clause
  481. // of the two argument OperandBuilders. The resulting ConditionBuilder can be
  482. // used as a part of other Condition Expressions or as an argument to the
  483. // WithCondition() method for the Builder struct.
  484. //
  485. // Example:
  486. //
  487. // // condition represents the greater than clause of the item attribute
  488. // // "foo" and the value 5
  489. // condition := expression.GreaterThan(expression.Name("foo"), expression.Value(5))
  490. //
  491. // // Used in another Condition Expression
  492. // anotherCondition := expression.Not(condition)
  493. // // Used to make an Builder
  494. // builder := expression.NewBuilder().WithCondition(condition)
  495. //
  496. // Expression Equivalent:
  497. //
  498. // expression.GreaterThan(expression.Name("foo"), expression.Value(5))
  499. // // Let :five be an ExpressionAttributeValue representing the value 5
  500. // "foo > :five"
  501. func GreaterThan(left, right OperandBuilder) ConditionBuilder {
  502. return ConditionBuilder{
  503. operandList: []OperandBuilder{left, right},
  504. mode: greaterThanCond,
  505. }
  506. }
  507. // GreaterThan returns a ConditionBuilder representing the greater than clause
  508. // of the two argument OperandBuilders. The resulting ConditionBuilder can be
  509. // used as a part of other Condition Expressions or as an argument to the
  510. // WithCondition() method for the Builder struct.
  511. //
  512. // Example:
  513. //
  514. // // condition represents the greater than clause of the item attribute
  515. // // "foo" and the value 5
  516. // condition := expression.Name("foo").GreaterThan(expression.Value(5))
  517. //
  518. // // Used in another Condition Expression
  519. // anotherCondition := expression.Not(condition)
  520. // // Used to make an Builder
  521. // builder := expression.NewBuilder().WithCondition(condition)
  522. //
  523. // Expression Equivalent:
  524. //
  525. // expression.Name("foo").GreaterThan(expression.Value(5))
  526. // // Let :five be an ExpressionAttributeValue representing the value 5
  527. // "foo > :five"
  528. func (nb NameBuilder) GreaterThan(right OperandBuilder) ConditionBuilder {
  529. return GreaterThan(nb, right)
  530. }
  531. // GreaterThan returns a ConditionBuilder representing the greater than clause
  532. // of the two argument OperandBuilders. The resulting ConditionBuilder can be
  533. // used as a part of other Condition Expressions or as an argument to the
  534. // WithCondition() method for the Builder struct.
  535. //
  536. // Example:
  537. //
  538. // // condition represents the greater than clause of the item attribute
  539. // // "foo" and the value 5
  540. // condition := expression.Value(5).GreaterThan(expression.Name("foo"))
  541. //
  542. // // Used in another Condition Expression
  543. // anotherCondition := expression.Not(condition)
  544. // // Used to make an Builder
  545. // builder := expression.NewBuilder().WithCondition(condition)
  546. //
  547. // Expression Equivalent:
  548. //
  549. // expression.Value(5).GreaterThan(expression.Name("foo"))
  550. // // Let :five be an ExpressionAttributeValue representing the value 5
  551. // ":five > foo"
  552. func (vb ValueBuilder) GreaterThan(right OperandBuilder) ConditionBuilder {
  553. return GreaterThan(vb, right)
  554. }
  555. // GreaterThan returns a ConditionBuilder representing the greater than
  556. // clause of the two argument OperandBuilders. The resulting ConditionBuilder
  557. // can be used as a part of other Condition Expressions or as an argument to the
  558. // WithCondition() method for the Builder struct.
  559. //
  560. // Example:
  561. //
  562. // // condition represents the greater than clause of the size of the item
  563. // // attribute "foo" and the value 5
  564. // condition := expression.Size(expression.Name("foo")).GreaterThan(expression.Value(5))
  565. //
  566. // // Used in another Condition Expression
  567. // anotherCondition := expression.Not(condition)
  568. // // Used to make an Builder
  569. // builder := expression.NewBuilder().WithCondition(condition)
  570. //
  571. // Expression Equivalent:
  572. //
  573. // expression.Size(expression.Name("foo")).GreaterThan(expression.Value(5))
  574. // // Let :five be an ExpressionAttributeValue representing the value 5
  575. // "size (foo) > :five"
  576. func (sb SizeBuilder) GreaterThan(right OperandBuilder) ConditionBuilder {
  577. return GreaterThan(sb, right)
  578. }
  579. // GreaterThanEqual returns a ConditionBuilder representing the greater than
  580. // equal to clause of the two argument OperandBuilders. The resulting
  581. // ConditionBuilder can be used as a part of other Condition Expressions or as
  582. // an argument to the WithCondition() method for the Builder struct.
  583. //
  584. // Example:
  585. //
  586. // // condition represents the greater than equal to clause of the item
  587. // // attribute "foo" and the value 5
  588. // condition := expression.GreaterThanEqual(expression.Name("foo"), expression.Value(5))
  589. //
  590. // // Used in another Condition Expression
  591. // anotherCondition := expression.Not(condition)
  592. // // Used to make an Builder
  593. // builder := expression.NewBuilder().WithCondition(condition)
  594. //
  595. // Expression Equivalent:
  596. //
  597. // expression.GreaterThanEqual(expression.Name("foo"), expression.Value(5))
  598. // // Let :five be an ExpressionAttributeValue representing the value 5
  599. // "foo >= :five"
  600. func GreaterThanEqual(left, right OperandBuilder) ConditionBuilder {
  601. return ConditionBuilder{
  602. operandList: []OperandBuilder{left, right},
  603. mode: greaterThanEqualCond,
  604. }
  605. }
  606. // GreaterThanEqual returns a ConditionBuilder representing the greater than
  607. // equal to clause of the two argument OperandBuilders. The resulting
  608. // ConditionBuilder can be used as a part of other Condition Expressions or as
  609. // an argument to the WithCondition() method for the Builder struct.
  610. //
  611. // Example:
  612. //
  613. // // condition represents the greater than equal to clause of the item
  614. // // attribute "foo" and the value 5
  615. // condition := expression.Name("foo").GreaterThanEqual(expression.Value(5))
  616. //
  617. // // Used in another Condition Expression
  618. // anotherCondition := expression.Not(condition)
  619. // // Used to make an Builder
  620. // builder := expression.NewBuilder().WithCondition(condition)
  621. //
  622. // Expression Equivalent:
  623. //
  624. // expression.Name("foo").GreaterThanEqual(expression.Value(5))
  625. // // Let :five be an ExpressionAttributeValue representing the value 5
  626. // "foo >= :five"
  627. func (nb NameBuilder) GreaterThanEqual(right OperandBuilder) ConditionBuilder {
  628. return GreaterThanEqual(nb, right)
  629. }
  630. // GreaterThanEqual returns a ConditionBuilder representing the greater than
  631. // equal to clause of the two argument OperandBuilders. The resulting
  632. // ConditionBuilder can be used as a part of other Condition Expressions or as
  633. // an argument to the WithCondition() method for the Builder struct.
  634. //
  635. // Example:
  636. //
  637. // // condition represents the greater than equal to clause of the item
  638. // // attribute "foo" and the value 5
  639. // condition := expression.Value(5).GreaterThanEqual(expression.Name("foo"))
  640. //
  641. // // Used in another Condition Expression
  642. // anotherCondition := expression.Not(condition)
  643. // // Used to make an Builder
  644. // builder := expression.NewBuilder().WithCondition(condition)
  645. //
  646. // Expression Equivalent:
  647. //
  648. // expression.Value(5).GreaterThanEqual(expression.Name("foo"))
  649. // // Let :five be an ExpressionAttributeValue representing the value 5
  650. // ":five >= foo"
  651. func (vb ValueBuilder) GreaterThanEqual(right OperandBuilder) ConditionBuilder {
  652. return GreaterThanEqual(vb, right)
  653. }
  654. // GreaterThanEqual returns a ConditionBuilder representing the greater than
  655. // equal to clause of the two argument OperandBuilders. The resulting
  656. // ConditionBuilder can be used as a part of other Condition Expressions or as
  657. // an argument to the WithCondition() method for the Builder struct.
  658. //
  659. // Example:
  660. //
  661. // // condition represents the greater than equal to clause of the size of
  662. // // the item attribute "foo" and the value 5
  663. // condition := expression.Size(expression.Name("foo")).GreaterThanEqual(expression.Value(5))
  664. //
  665. // // Used in another Condition Expression
  666. // anotherCondition := expression.Not(condition)
  667. // // Used to make an Builder
  668. // builder := expression.NewBuilder().WithCondition(condition)
  669. //
  670. // Expression Equivalent:
  671. //
  672. // expression.Size(expression.Name("foo")).GreaterThanEqual(expression.Value(5))
  673. // // Let :five be an ExpressionAttributeValue representing the value 5
  674. // "size (foo) >= :five"
  675. func (sb SizeBuilder) GreaterThanEqual(right OperandBuilder) ConditionBuilder {
  676. return GreaterThanEqual(sb, right)
  677. }
  678. // And returns a ConditionBuilder representing the logical AND clause of the
  679. // argument ConditionBuilders. The resulting ConditionBuilder can be used as a
  680. // part of other Condition Expressions or as an argument to the WithCondition()
  681. // method for the Builder struct. Note that And() can take a variadic number of
  682. // ConditionBuilders as arguments.
  683. //
  684. // Example:
  685. //
  686. // // condition represents the condition where the item attribute "Name" is
  687. // // equal to value "Generic Name" AND the item attribute "Age" is less
  688. // // than value 40
  689. // condition := expression.And(expression.Name("Name").Equal(expression.Value("Generic Name")), expression.Name("Age").LessThan(expression.Value(40)))
  690. //
  691. // // Used in another Condition Expression
  692. // anotherCondition := expression.Not(condition)
  693. // // Used to make an Builder
  694. // builder := expression.NewBuilder().WithCondition(condition)
  695. //
  696. // Expression Equivalent:
  697. //
  698. // expression.And(expression.Name("Name").Equal(expression.Value("Generic Name")), expression.Name("Age").LessThan(expression.Value(40)))
  699. // // Let #NAME, :name, and :forty be ExpressionAttributeName and
  700. // // ExpressionAttributeValues representing the item attribute "Name", the
  701. // // value "Generic Name", and the value 40
  702. // "(#NAME = :name) AND (Age < :forty)"
  703. func And(left, right ConditionBuilder, other ...ConditionBuilder) ConditionBuilder {
  704. other = append([]ConditionBuilder{left, right}, other...)
  705. return ConditionBuilder{
  706. conditionList: other,
  707. mode: andCond,
  708. }
  709. }
  710. // And returns a ConditionBuilder representing the logical AND clause of the
  711. // argument ConditionBuilders. The resulting ConditionBuilder can be used as a
  712. // part of other Condition Expressions or as an argument to the WithCondition()
  713. // method for the Builder struct. Note that And() can take a variadic number of
  714. // ConditionBuilders as arguments.
  715. //
  716. // Example:
  717. //
  718. // // condition represents the condition where the item attribute "Name" is
  719. // // equal to value "Generic Name" AND the item attribute "Age" is less
  720. // // than value 40
  721. // condition := expression.Name("Name").Equal(expression.Value("Generic Name")).And(expression.Name("Age").LessThan(expression.Value(40)))
  722. //
  723. // // Used in another Condition Expression
  724. // anotherCondition := expression.Not(condition)
  725. // // Used to make an Builder
  726. // builder := expression.NewBuilder().WithCondition(condition)
  727. //
  728. // Expression Equivalent:
  729. //
  730. // expression.Name("Name").Equal(expression.Value("Generic Name")).And(expression.Name("Age").LessThan(expression.Value(40)))
  731. // // Let #NAME, :name, and :forty be ExpressionAttributeName and
  732. // // ExpressionAttributeValues representing the item attribute "Name", the
  733. // // value "Generic Name", and the value 40
  734. // "(#NAME = :name) AND (Age < :forty)"
  735. func (cb ConditionBuilder) And(right ConditionBuilder, other ...ConditionBuilder) ConditionBuilder {
  736. return And(cb, right, other...)
  737. }
  738. // Or returns a ConditionBuilder representing the logical OR clause of the
  739. // argument ConditionBuilders. The resulting ConditionBuilder can be used as a
  740. // part of other Condition Expressions or as an argument to the WithCondition()
  741. // method for the Builder struct. Note that Or() can take a variadic number of
  742. // ConditionBuilders as arguments.
  743. //
  744. // Example:
  745. //
  746. // // condition represents the condition where the item attribute "Price" is
  747. // // less than the value 100 OR the item attribute "Rating" is greater than
  748. // // the value 8
  749. // condition := expression.Or(expression.Name("Price").Equal(expression.Value(100)), expression.Name("Rating").LessThan(expression.Value(8)))
  750. //
  751. // // Used in another Condition Expression
  752. // anotherCondition := expression.Not(condition)
  753. // // Used to make an Builder
  754. // builder := expression.NewBuilder().WithCondition(condition)
  755. //
  756. // Expression Equivalent:
  757. //
  758. // expression.Or(expression.Name("Price").Equal(expression.Value(100)), expression.Name("Rating").LessThan(expression.Value(8)))
  759. // // Let :price and :rating be ExpressionAttributeValues representing the
  760. // // the value 100 and value 8 respectively
  761. // "(Price < :price) OR (Rating > :rating)"
  762. func Or(left, right ConditionBuilder, other ...ConditionBuilder) ConditionBuilder {
  763. other = append([]ConditionBuilder{left, right}, other...)
  764. return ConditionBuilder{
  765. conditionList: other,
  766. mode: orCond,
  767. }
  768. }
  769. // Or returns a ConditionBuilder representing the logical OR clause of the
  770. // argument ConditionBuilders. The resulting ConditionBuilder can be used as a
  771. // part of other Condition Expressions or as an argument to the WithCondition()
  772. // method for the Builder struct. Note that Or() can take a variadic number of
  773. // ConditionBuilders as arguments.
  774. //
  775. // Example:
  776. //
  777. // // condition represents the condition where the item attribute "Price" is
  778. // // less than the value 100 OR the item attribute "Rating" is greater than
  779. // // the value 8
  780. // condition := expression.Name("Price").Equal(expression.Value(100)).Or(expression.Name("Rating").LessThan(expression.Value(8)))
  781. //
  782. // // Used in another Condition Expression
  783. // anotherCondition := expression.Not(condition)
  784. // // Used to make an Builder
  785. // builder := expression.NewBuilder().WithCondition(condition)
  786. //
  787. // Expression Equivalent:
  788. //
  789. // expression.Name("Price").Equal(expression.Value(100)).Or(expression.Name("Rating").LessThan(expression.Value(8)))
  790. // // Let :price and :rating be ExpressionAttributeValues representing the
  791. // // the value 100 and value 8 respectively
  792. // "(Price < :price) OR (Rating > :rating)"
  793. func (cb ConditionBuilder) Or(right ConditionBuilder, other ...ConditionBuilder) ConditionBuilder {
  794. return Or(cb, right, other...)
  795. }
  796. // Not returns a ConditionBuilder representing the logical NOT clause of the
  797. // argument ConditionBuilder. The resulting ConditionBuilder can be used as a
  798. // part of other Condition Expressions or as an argument to the WithCondition()
  799. // method for the Builder struct.
  800. //
  801. // Example:
  802. //
  803. // // condition represents the condition where the item attribute "Name"
  804. // // does not begin with "test"
  805. // condition := expression.Not(expression.Name("Name").BeginsWith("test"))
  806. //
  807. // // Used in another Condition Expression
  808. // anotherCondition := expression.Not(condition)
  809. // // Used to make an Builder
  810. // builder := expression.NewBuilder().WithCondition(condition)
  811. //
  812. // Expression Equivalent:
  813. //
  814. // expression.Not(expression.Name("Name").BeginsWith("test"))
  815. // // Let :prefix be an ExpressionAttributeValue representing the value
  816. // // "test"
  817. // "NOT (begins_with (:prefix))"
  818. func Not(conditionBuilder ConditionBuilder) ConditionBuilder {
  819. return ConditionBuilder{
  820. conditionList: []ConditionBuilder{conditionBuilder},
  821. mode: notCond,
  822. }
  823. }
  824. // Not returns a ConditionBuilder representing the logical NOT clause of the
  825. // argument ConditionBuilder. The resulting ConditionBuilder can be used as a
  826. // part of other Condition Expressions or as an argument to the WithCondition()
  827. // method for the Builder struct.
  828. //
  829. // Example:
  830. //
  831. // // condition represents the condition where the item attribute "Name"
  832. // // does not begin with "test"
  833. // condition := expression.Name("Name").BeginsWith("test").Not()
  834. //
  835. // // Used in another Condition Expression
  836. // anotherCondition := expression.Not(condition)
  837. // // Used to make an Builder
  838. // builder := expression.NewBuilder().WithCondition(condition)
  839. //
  840. // Expression Equivalent:
  841. //
  842. // expression.Name("Name").BeginsWith("test").Not()
  843. // // Let :prefix be an ExpressionAttributeValue representing the value
  844. // // "test"
  845. // "NOT (begins_with (:prefix))"
  846. func (cb ConditionBuilder) Not() ConditionBuilder {
  847. return Not(cb)
  848. }
  849. // Between returns a ConditionBuilder representing the result of the
  850. // BETWEEN function in DynamoDB Condition Expressions. The resulting
  851. // ConditionBuilder can be used as a part of other Condition Expressions or as
  852. // an argument to the WithCondition() method for the Builder struct.
  853. //
  854. // Example:
  855. //
  856. // // condition represents the condition where the value of the item
  857. // // attribute "Rating" is between values 5 and 10
  858. // condition := expression.Between(expression.Name("Rating"), expression.Value(5), expression.Value(10))
  859. //
  860. // // Used in another Condition Expression
  861. // anotherCondition := expression.Not(condition)
  862. // // Used to make an Builder
  863. // builder := expression.NewBuilder().WithCondition(condition)
  864. //
  865. // Expression Equivalent:
  866. //
  867. // expression.Between(expression.Name("Rating"), expression.Value(5), expression.Value(10))
  868. // // Let :five and :ten be ExpressionAttributeValues representing the value
  869. // // 5 and the value 10
  870. // "Rating BETWEEN :five AND :ten"
  871. func Between(op, lower, upper OperandBuilder) ConditionBuilder {
  872. return ConditionBuilder{
  873. operandList: []OperandBuilder{op, lower, upper},
  874. mode: betweenCond,
  875. }
  876. }
  877. // Between returns a ConditionBuilder representing the result of the
  878. // BETWEEN function in DynamoDB Condition Expressions. The resulting
  879. // ConditionBuilder can be used as a part of other Condition Expressions or as
  880. // an argument to the WithCondition() method for the Builder struct.
  881. //
  882. // Example:
  883. //
  884. // // condition represents the condition where the value of the item
  885. // // attribute "Rating" is between values 5 and 10
  886. // condition := expression.Name("Rating").Between(expression.Value(5), expression.Value(10))
  887. //
  888. // // Used in another Condition Expression
  889. // anotherCondition := expression.Not(condition)
  890. // // Used to make an Builder
  891. // builder := expression.NewBuilder().WithCondition(condition)
  892. //
  893. // Expression Equivalent:
  894. //
  895. // expression.Name("Rating").Between(expression.Value(5), expression.Value(10))
  896. // // Let :five and :ten be ExpressionAttributeValues representing the value
  897. // // 5 and the value 10
  898. // "Rating BETWEEN :five AND :ten"
  899. func (nb NameBuilder) Between(lower, upper OperandBuilder) ConditionBuilder {
  900. return Between(nb, lower, upper)
  901. }
  902. // Between returns a ConditionBuilder representing the result of the
  903. // BETWEEN function in DynamoDB Condition Expressions. The resulting
  904. // ConditionBuilder can be used as a part of other Condition Expressions or as
  905. // an argument to the WithCondition() method for the Builder struct.
  906. //
  907. // Example:
  908. //
  909. // // condition represents the condition where the value 6 is between values
  910. // // 5 and 10
  911. // condition := expression.Value(6).Between(expression.Value(5), expression.Value(10))
  912. //
  913. // // Used in another Condition Expression
  914. // anotherCondition := expression.Not(condition)
  915. // // Used to make an Builder
  916. // builder := expression.NewBuilder().WithCondition(condition)
  917. //
  918. // Expression Equivalent:
  919. //
  920. // expression.Value(6).Between(expression.Value(5), expression.Value(10))
  921. // // Let :six, :five and :ten be ExpressionAttributeValues representing the
  922. // // values 6, 5, and 10 respectively
  923. // ":six BETWEEN :five AND :ten"
  924. func (vb ValueBuilder) Between(lower, upper OperandBuilder) ConditionBuilder {
  925. return Between(vb, lower, upper)
  926. }
  927. // Between returns a ConditionBuilder representing the result of the
  928. // BETWEEN function in DynamoDB Condition Expressions. The resulting
  929. // ConditionBuilder can be used as a part of other Condition Expressions or as
  930. // an argument to the WithCondition() method for the Builder struct.
  931. //
  932. // Example:
  933. //
  934. // // condition represents the condition where the size of the item
  935. // // attribute "InviteList" is between values 5 and 10
  936. // condition := expression.Size(expression.Name("InviteList")).Between(expression.Value(5), expression.Value(10))
  937. //
  938. // // Used in another Condition Expression
  939. // anotherCondition := expression.Not(condition)
  940. // // Used to make an Builder
  941. // builder := expression.NewBuilder().WithCondition(condition)
  942. //
  943. // Expression Equivalent:
  944. //
  945. // expression.Size(expression.Name("InviteList")).Between(expression.Value(5), expression.Value(10))
  946. // // Let :five and :ten be ExpressionAttributeValues representing the value
  947. // // 5 and the value 10
  948. // "size (InviteList) BETWEEN :five AND :ten"
  949. func (sb SizeBuilder) Between(lower, upper OperandBuilder) ConditionBuilder {
  950. return Between(sb, lower, upper)
  951. }
  952. // In returns a ConditionBuilder representing the result of the IN function
  953. // in DynamoDB Condition Expressions. The resulting ConditionBuilder can be used
  954. // as a part of other Condition Expressions or as an argument to the
  955. // WithCondition() method for the Builder struct.
  956. //
  957. // Example:
  958. //
  959. // // condition represents the condition where the value of the item
  960. // // attribute "Color" is checked against the list of colors "red",
  961. // // "green", and "blue".
  962. // condition := expression.In(expression.Name("Color"), expression.Value("red"), expression.Value("green"), expression.Value("blue"))
  963. //
  964. // // Used in another Condition Expression
  965. // anotherCondition := expression.Not(condition)
  966. // // Used to make an Builder
  967. // builder := expression.NewBuilder().WithCondition(condition)
  968. //
  969. // Expression Equivalent:
  970. //
  971. // expression.In(expression.Name("Color"), expression.Value("red"), expression.Value("green"), expression.Value("blue"))
  972. // // Let :red, :green, :blue be ExpressionAttributeValues representing the
  973. // // values "red", "green", and "blue" respectively
  974. // "Color IN (:red, :green, :blue)"
  975. func In(left, right OperandBuilder, other ...OperandBuilder) ConditionBuilder {
  976. other = append([]OperandBuilder{left, right}, other...)
  977. return ConditionBuilder{
  978. operandList: other,
  979. mode: inCond,
  980. }
  981. }
  982. // In returns a ConditionBuilder representing the result of the IN function
  983. // in DynamoDB Condition Expressions. The resulting ConditionBuilder can be used
  984. // as a part of other Condition Expressions or as an argument to the
  985. // WithCondition() method for the Builder struct.
  986. //
  987. // Example:
  988. //
  989. // // condition represents the condition where the value of the item
  990. // // attribute "Color" is checked against the list of colors "red",
  991. // // "green", and "blue".
  992. // condition := expression.Name("Color").In(expression.Value("red"), expression.Value("green"), expression.Value("blue"))
  993. //
  994. // // Used in another Condition Expression
  995. // anotherCondition := expression.Not(condition)
  996. // // Used to make an Builder
  997. // builder := expression.NewBuilder().WithCondition(condition)
  998. //
  999. // Expression Equivalent:
  1000. //
  1001. // expression.Name("Color").In(expression.Value("red"), expression.Value("green"), expression.Value("blue"))
  1002. // // Let :red, :green, :blue be ExpressionAttributeValues representing the
  1003. // // values "red", "green", and "blue" respectively
  1004. // "Color IN (:red, :green, :blue)"
  1005. func (nb NameBuilder) In(right OperandBuilder, other ...OperandBuilder) ConditionBuilder {
  1006. return In(nb, right, other...)
  1007. }
  1008. // In returns a ConditionBuilder representing the result of the IN function
  1009. // TODO change this one
  1010. // in DynamoDB Condition Expressions. The resulting ConditionBuilder can be used
  1011. // as a part of other Condition Expressions or as an argument to the
  1012. // WithCondition() method for the Builder struct.
  1013. //
  1014. // Example:
  1015. //
  1016. // // condition represents the condition where the value "yellow" is checked
  1017. // // against the list of colors "red", "green", and "blue".
  1018. // condition := expression.Value("yellow").In(expression.Value("red"), expression.Value("green"), expression.Value("blue"))
  1019. //
  1020. // // Used in another Condition Expression
  1021. // anotherCondition := expression.Not(condition)
  1022. // // Used to make an Builder
  1023. // builder := expression.NewBuilder().WithCondition(condition)
  1024. //
  1025. // Expression Equivalent:
  1026. //
  1027. // expression.Value("yellow").In(expression.Value("red"), expression.Value("green"), expression.Value("blue"))
  1028. // // Let :yellow, :red, :green, :blue be ExpressionAttributeValues
  1029. // // representing the values "yellow", "red", "green", and "blue"
  1030. // // respectively
  1031. // ":yellow IN (:red, :green, :blue)"
  1032. func (vb ValueBuilder) In(right OperandBuilder, other ...OperandBuilder) ConditionBuilder {
  1033. return In(vb, right, other...)
  1034. }
  1035. // In returns a ConditionBuilder representing the result of the IN function
  1036. // in DynamoDB Condition Expressions. The resulting ConditionBuilder can be used
  1037. // as a part of other Condition Expressions or as an argument to the
  1038. // WithCondition() method for the Builder struct.
  1039. //
  1040. // Example:
  1041. //
  1042. // // condition represents the condition where the size of the item
  1043. // // attribute "Donuts" is checked against the list of numbers 12, 24, and
  1044. // // 36.
  1045. // condition := expression.Size(expression.Name("Donuts")).In(expression.Value(12), expression.Value(24), expression.Value(36))
  1046. //
  1047. // // Used in another Condition Expression
  1048. // anotherCondition := expression.Not(condition)
  1049. // // Used to make an Builder
  1050. // builder := expression.NewBuilder().WithCondition(condition)
  1051. //
  1052. // Expression Equivalent:
  1053. //
  1054. // expression.Size(expression.Name("Donuts")).In(expression.Value(12), expression.Value(24), expression.Value(36))
  1055. // // Let :dozen, :twoDozen, :threeDozen be ExpressionAttributeValues
  1056. // // representing the values 12, 24, and 36 respectively
  1057. // "size (Donuts) IN (12, 24, 36)"
  1058. func (sb SizeBuilder) In(right OperandBuilder, other ...OperandBuilder) ConditionBuilder {
  1059. return In(sb, right, other...)
  1060. }
  1061. // AttributeExists returns a ConditionBuilder representing the result of the
  1062. // attribute_exists function in DynamoDB Condition Expressions. The resulting
  1063. // ConditionBuilder can be used as a part of other Condition Expressions or as
  1064. // an argument to the WithCondition() method for the Builder struct.
  1065. //
  1066. // Example:
  1067. //
  1068. // // condition represents the boolean condition of whether the item
  1069. // // attribute "Age" exists or not
  1070. // condition := expression.AttributeExists(expression.Name("Age"))
  1071. //
  1072. // // Used in another Condition Expression
  1073. // anotherCondition := expression.Not(condition)
  1074. // // Used to make an Builder
  1075. // builder := expression.NewBuilder().WithCondition(condition)
  1076. //
  1077. // Expression Equivalent:
  1078. //
  1079. // expression.AttributeExists(expression.Name("Age"))
  1080. // "attribute_exists (Age))"
  1081. func AttributeExists(nameBuilder NameBuilder) ConditionBuilder {
  1082. return ConditionBuilder{
  1083. operandList: []OperandBuilder{nameBuilder},
  1084. mode: attrExistsCond,
  1085. }
  1086. }
  1087. // AttributeExists returns a ConditionBuilder representing the result of the
  1088. // attribute_exists function in DynamoDB Condition Expressions. The resulting
  1089. // ConditionBuilder can be used as a part of other Condition Expressions or as
  1090. // an argument to the WithCondition() method for the Builder struct.
  1091. //
  1092. // Example:
  1093. //
  1094. // // condition represents the boolean condition of whether the item
  1095. // // attribute "Age" exists or not
  1096. // condition := expression.Name("Age").AttributeExists()
  1097. //
  1098. // // Used in another Condition Expression
  1099. // anotherCondition := expression.Not(condition)
  1100. // // Used to make an Builder
  1101. // builder := expression.NewBuilder().WithCondition(condition)
  1102. //
  1103. // Expression Equivalent:
  1104. //
  1105. // expression.Name("Age").AttributeExists()
  1106. // "attribute_exists (Age))"
  1107. func (nb NameBuilder) AttributeExists() ConditionBuilder {
  1108. return AttributeExists(nb)
  1109. }
  1110. // AttributeNotExists returns a ConditionBuilder representing the result of
  1111. // the attribute_not_exists function in DynamoDB Condition Expressions. The
  1112. // resulting ConditionBuilder can be used as a part of other Condition
  1113. // Expressions or as an argument to the WithCondition() method for the Builder
  1114. // struct.
  1115. //
  1116. // Example:
  1117. //
  1118. // // condition represents the boolean condition of whether the item
  1119. // // attribute "Age" exists or not
  1120. // condition := expression.AttributeNotExists(expression.Name("Age"))
  1121. //
  1122. // // Used in another Condition Expression
  1123. // anotherCondition := expression.Not(condition)
  1124. // // Used to make an Builder
  1125. // builder := expression.NewBuilder().WithCondition(condition)
  1126. //
  1127. // Expression Equivalent:
  1128. //
  1129. // expression.AttributeNotExists(expression.Name("Age"))
  1130. // "attribute_not_exists (Age))"
  1131. func AttributeNotExists(nameBuilder NameBuilder) ConditionBuilder {
  1132. return ConditionBuilder{
  1133. operandList: []OperandBuilder{nameBuilder},
  1134. mode: attrNotExistsCond,
  1135. }
  1136. }
  1137. // AttributeNotExists returns a ConditionBuilder representing the result of
  1138. // the attribute_not_exists function in DynamoDB Condition Expressions. The
  1139. // resulting ConditionBuilder can be used as a part of other Condition
  1140. // Expressions or as an argument to the WithCondition() method for the Builder
  1141. // struct.
  1142. //
  1143. // Example:
  1144. //
  1145. // // condition represents the boolean condition of whether the item
  1146. // // attribute "Age" exists or not
  1147. // condition := expression.Name("Age").AttributeNotExists()
  1148. //
  1149. // // Used in another Condition Expression
  1150. // anotherCondition := expression.Not(condition)
  1151. // // Used to make an Builder
  1152. // builder := expression.NewBuilder().WithCondition(condition)
  1153. //
  1154. // Expression Equivalent:
  1155. //
  1156. // expression.Name("Age").AttributeNotExists()
  1157. // "attribute_not_exists (Age))"
  1158. func (nb NameBuilder) AttributeNotExists() ConditionBuilder {
  1159. return AttributeNotExists(nb)
  1160. }
  1161. // AttributeType returns a ConditionBuilder representing the result of the
  1162. // attribute_type function in DynamoDB Condition Expressions. The DynamoDB types
  1163. // are represented by the type DynamoDBAttributeType. The resulting
  1164. // ConditionBuilder can be used as a part of other Condition Expressions or as
  1165. // an argument to the WithCondition() method for the Builder struct.
  1166. //
  1167. // Example:
  1168. //
  1169. // // condition represents the boolean condition of whether the item
  1170. // // attribute "Age" has the DynamoDB type Number or not
  1171. // condition := expression.AttributeType(expression.Name("Age"), expression.Number)
  1172. //
  1173. // // Used in another Condition Expression
  1174. // anotherCondition := expression.Not(condition)
  1175. // // Used to make an Builder
  1176. // builder := expression.NewBuilder().WithCondition(condition)
  1177. //
  1178. // Expression Equivalent:
  1179. //
  1180. // expression.AttributeType(expression.Name("Age"), expression.Number)
  1181. // // Let :type be an ExpressionAttributeValue representing the value "N"
  1182. // "attribute_type (Age, :type)"
  1183. func AttributeType(nameBuilder NameBuilder, attributeType DynamoDBAttributeType) ConditionBuilder {
  1184. v := ValueBuilder{
  1185. value: string(attributeType),
  1186. }
  1187. return ConditionBuilder{
  1188. operandList: []OperandBuilder{nameBuilder, v},
  1189. mode: attrTypeCond,
  1190. }
  1191. }
  1192. // AttributeType returns a ConditionBuilder representing the result of the
  1193. // attribute_type function in DynamoDB Condition Expressions. The DynamoDB types
  1194. // are represented by the type DynamoDBAttributeType. The resulting
  1195. // ConditionBuilder can be used as a part of other Condition Expressions or as
  1196. // an argument to the WithCondition() method for the Builder struct.
  1197. //
  1198. // Example:
  1199. //
  1200. // // condition represents the boolean condition of whether the item
  1201. // // attribute "Age" has the DynamoDB type Number or not
  1202. // condition := expression.Name("Age").AttributeType(expression.Number)
  1203. //
  1204. // // Used in another Condition Expression
  1205. // anotherCondition := expression.Not(condition)
  1206. // // Used to make an Builder
  1207. // builder := expression.NewBuilder().WithCondition(condition)
  1208. //
  1209. // Expression Equivalent:
  1210. //
  1211. // expression.Name("Age").AttributeType(expression.Number)
  1212. // // Let :type be an ExpressionAttributeValue representing the value "N"
  1213. // "attribute_type (Age, :type)"
  1214. func (nb NameBuilder) AttributeType(attributeType DynamoDBAttributeType) ConditionBuilder {
  1215. return AttributeType(nb, attributeType)
  1216. }
  1217. // BeginsWith returns a ConditionBuilder representing the result of the
  1218. // begins_with function in DynamoDB Condition Expressions. The resulting
  1219. // ConditionBuilder can be used as a part of other Condition Expressions or as
  1220. // an argument to the WithCondition() method for the Builder struct.
  1221. //
  1222. // Example:
  1223. //
  1224. // // condition represents the boolean condition of whether the item
  1225. // // attribute "CodeName" starts with the substring "Ben"
  1226. // condition := expression.BeginsWith(expression.Name("CodeName"), "Ben")
  1227. //
  1228. // // Used in another Condition Expression
  1229. // anotherCondition := expression.Not(condition)
  1230. // // Used to make an Builder
  1231. // builder := expression.NewBuilder().WithCondition(condition)
  1232. //
  1233. // Expression Equivalent:
  1234. //
  1235. // expression.BeginsWith(expression.Name("CodeName"), "Ben")
  1236. // // Let :ben be an ExpressionAttributeValue representing the value "Ben"
  1237. // "begins_with (CodeName, :ben)"
  1238. func BeginsWith(nameBuilder NameBuilder, prefix string) ConditionBuilder {
  1239. v := ValueBuilder{
  1240. value: prefix,
  1241. }
  1242. return ConditionBuilder{
  1243. operandList: []OperandBuilder{nameBuilder, v},
  1244. mode: beginsWithCond,
  1245. }
  1246. }
  1247. // BeginsWith returns a ConditionBuilder representing the result of the
  1248. // begins_with function in DynamoDB Condition Expressions. The resulting
  1249. // ConditionBuilder can be used as a part of other Condition Expressions or as
  1250. // an argument to the WithCondition() method for the Builder struct.
  1251. //
  1252. // Example:
  1253. //
  1254. // // condition represents the boolean condition of whether the item
  1255. // // attribute "CodeName" starts with the substring "Ben"
  1256. // condition := expression.Name("CodeName").BeginsWith("Ben")
  1257. //
  1258. // // Used in another Condition Expression
  1259. // anotherCondition := expression.Not(condition)
  1260. // // Used to make an Builder
  1261. // builder := expression.NewBuilder().WithCondition(condition)
  1262. //
  1263. // Expression Equivalent:
  1264. //
  1265. // expression.Name("CodeName").BeginsWith("Ben")
  1266. // // Let :ben be an ExpressionAttributeValue representing the value "Ben"
  1267. // "begins_with (CodeName, :ben)"
  1268. func (nb NameBuilder) BeginsWith(prefix string) ConditionBuilder {
  1269. return BeginsWith(nb, prefix)
  1270. }
  1271. // Contains returns a ConditionBuilder representing the result of the
  1272. // contains function in DynamoDB Condition Expressions. The resulting
  1273. // ConditionBuilder can be used as a part of other Condition Expressions or as
  1274. // an argument to the WithCondition() method for the Builder struct.
  1275. //
  1276. // Example:
  1277. //
  1278. // // condition represents the boolean condition of whether the item
  1279. // // attribute "InviteList" has the value "Ben"
  1280. // condition := expression.Contains(expression.Name("InviteList"), expression.Value("Ben"))
  1281. //
  1282. // // Used in another Condition Expression
  1283. // anotherCondition := expression.Not(condition)
  1284. // // Used to make an Builder
  1285. // builder := expression.NewBuilder().WithCondition(condition)
  1286. //
  1287. // Expression Equivalent:
  1288. //
  1289. // expression.Contains(expression.Name("InviteList"), expression.Value("Ben"))
  1290. // // Let :ben be an ExpressionAttributeValue representing the value "Ben"
  1291. // "contains (InviteList, :ben)"
  1292. func Contains(nameBuilder NameBuilder, substr string) ConditionBuilder {
  1293. v := ValueBuilder{
  1294. value: substr,
  1295. }
  1296. return ConditionBuilder{
  1297. operandList: []OperandBuilder{nameBuilder, v},
  1298. mode: containsCond,
  1299. }
  1300. }
  1301. // Contains returns a ConditionBuilder representing the result of the
  1302. // contains function in DynamoDB Condition Expressions. The resulting
  1303. // ConditionBuilder can be used as a part of other Condition Expressions or as
  1304. // an argument to the WithCondition() method for the Builder struct.
  1305. //
  1306. // Example:
  1307. //
  1308. // // condition represents the boolean condition of whether the item
  1309. // // attribute "InviteList" has the value "Ben"
  1310. // condition := expression.Name("InviteList").Contains(expression.Value("Ben"))
  1311. //
  1312. // // Used in another Condition Expression
  1313. // anotherCondition := expression.Not(condition)
  1314. // // Used to make an Builder
  1315. // builder := expression.NewBuilder().WithCondition(condition)
  1316. //
  1317. // Expression Equivalent:
  1318. //
  1319. // expression.Name("InviteList").Contains(expression.Value("Ben"))
  1320. // // Let :ben be an ExpressionAttributeValue representing the value "Ben"
  1321. // "contains (InviteList, :ben)"
  1322. func (nb NameBuilder) Contains(substr string) ConditionBuilder {
  1323. return Contains(nb, substr)
  1324. }
  1325. // buildTree builds a tree structure of exprNodes based on the tree
  1326. // structure of the input ConditionBuilder's child ConditionBuilders and
  1327. // OperandBuilders. buildTree() satisfies the treeBuilder interface so
  1328. // ConditionBuilder can be a part of Builder and Expression struct.
  1329. func (cb ConditionBuilder) buildTree() (exprNode, error) {
  1330. childNodes, err := cb.buildChildNodes()
  1331. if err != nil {
  1332. return exprNode{}, err
  1333. }
  1334. ret := exprNode{
  1335. children: childNodes,
  1336. }
  1337. switch cb.mode {
  1338. case equalCond, notEqualCond, lessThanCond, lessThanEqualCond, greaterThanCond, greaterThanEqualCond:
  1339. return compareBuildCondition(cb.mode, ret)
  1340. case andCond, orCond:
  1341. return compoundBuildCondition(cb, ret)
  1342. case notCond:
  1343. return notBuildCondition(ret)
  1344. case betweenCond:
  1345. return betweenBuildCondition(ret)
  1346. case inCond:
  1347. return inBuildCondition(cb, ret)
  1348. case attrExistsCond:
  1349. return attrExistsBuildCondition(ret)
  1350. case attrNotExistsCond:
  1351. return attrNotExistsBuildCondition(ret)
  1352. case attrTypeCond:
  1353. return attrTypeBuildCondition(ret)
  1354. case beginsWithCond:
  1355. return beginsWithBuildCondition(ret)
  1356. case containsCond:
  1357. return containsBuildCondition(ret)
  1358. case unsetCond:
  1359. return exprNode{}, newUnsetParameterError("buildTree", "ConditionBuilder")
  1360. default:
  1361. return exprNode{}, fmt.Errorf("build condition error: unsupported mode: %v", cb.mode)
  1362. }
  1363. }
  1364. // compareBuildCondition is the function to make exprNodes from Compare
  1365. // ConditionBuilders. compareBuildCondition is only called by the
  1366. // buildTree method. This function assumes that the argument ConditionBuilder
  1367. // has the right format.
  1368. func compareBuildCondition(conditionMode conditionMode, node exprNode) (exprNode, error) {
  1369. // Create a string with special characters that can be substituted later: $c
  1370. switch conditionMode {
  1371. case equalCond:
  1372. node.fmtExpr = "$c = $c"
  1373. case notEqualCond:
  1374. node.fmtExpr = "$c <> $c"
  1375. case lessThanCond:
  1376. node.fmtExpr = "$c < $c"
  1377. case lessThanEqualCond:
  1378. node.fmtExpr = "$c <= $c"
  1379. case greaterThanCond:
  1380. node.fmtExpr = "$c > $c"
  1381. case greaterThanEqualCond:
  1382. node.fmtExpr = "$c >= $c"
  1383. default:
  1384. return exprNode{}, fmt.Errorf("build compare condition error: unsupported mode: %v", conditionMode)
  1385. }
  1386. return node, nil
  1387. }
  1388. // compoundBuildCondition is the function to make exprNodes from And/Or
  1389. // ConditionBuilders. compoundBuildCondition is only called by the
  1390. // buildTree method. This function assumes that the argument ConditionBuilder
  1391. // has the right format.
  1392. func compoundBuildCondition(conditionBuilder ConditionBuilder, node exprNode) (exprNode, error) {
  1393. // create a string with escaped characters to substitute them with proper
  1394. // aliases during runtime
  1395. var mode string
  1396. switch conditionBuilder.mode {
  1397. case andCond:
  1398. mode = " AND "
  1399. case orCond:
  1400. mode = " OR "
  1401. default:
  1402. return exprNode{}, fmt.Errorf("build compound condition error: unsupported mode: %v", conditionBuilder.mode)
  1403. }
  1404. node.fmtExpr = "($c)" + strings.Repeat(mode+"($c)", len(conditionBuilder.conditionList)-1)
  1405. return node, nil
  1406. }
  1407. // notBuildCondition is the function to make exprNodes from Not
  1408. // ConditionBuilders. notBuildCondition is only called by the
  1409. // buildTree method. This function assumes that the argument ConditionBuilder
  1410. // has the right format.
  1411. func notBuildCondition(node exprNode) (exprNode, error) {
  1412. // create a string with escaped characters to substitute them with proper
  1413. // aliases during runtime
  1414. node.fmtExpr = "NOT ($c)"
  1415. return node, nil
  1416. }
  1417. // betweenBuildCondition is the function to make exprNodes from Between
  1418. // ConditionBuilders. BuildCondition is only called by the
  1419. // buildTree method. This function assumes that the argument ConditionBuilder
  1420. // has the right format.
  1421. func betweenBuildCondition(node exprNode) (exprNode, error) {
  1422. // Create a string with special characters that can be substituted later: $c
  1423. node.fmtExpr = "$c BETWEEN $c AND $c"
  1424. return node, nil
  1425. }
  1426. // inBuildCondition is the function to make exprNodes from In
  1427. // ConditionBuilders. inBuildCondition is only called by the
  1428. // buildTree method. This function assumes that the argument ConditionBuilder
  1429. // has the right format.
  1430. func inBuildCondition(conditionBuilder ConditionBuilder, node exprNode) (exprNode, error) {
  1431. // Create a string with special characters that can be substituted later: $c
  1432. node.fmtExpr = "$c IN ($c" + strings.Repeat(", $c", len(conditionBuilder.operandList)-2) + ")"
  1433. return node, nil
  1434. }
  1435. // attrExistsBuildCondition is the function to make exprNodes from
  1436. // AttrExistsCond ConditionBuilders. attrExistsBuildCondition is only
  1437. // called by the buildTree method. This function assumes that the argument
  1438. // ConditionBuilder has the right format.
  1439. func attrExistsBuildCondition(node exprNode) (exprNode, error) {
  1440. // Create a string with special characters that can be substituted later: $c
  1441. node.fmtExpr = "attribute_exists ($c)"
  1442. return node, nil
  1443. }
  1444. // attrNotExistsBuildCondition is the function to make exprNodes from
  1445. // AttrNotExistsCond ConditionBuilders. attrNotExistsBuildCondition is only
  1446. // called by the buildTree method. This function assumes that the argument
  1447. // ConditionBuilder has the right format.
  1448. func attrNotExistsBuildCondition(node exprNode) (exprNode, error) {
  1449. // Create a string with special characters that can be substituted later: $c
  1450. node.fmtExpr = "attribute_not_exists ($c)"
  1451. return node, nil
  1452. }
  1453. // attrTypeBuildCondition is the function to make exprNodes from AttrTypeCond
  1454. // ConditionBuilders. attrTypeBuildCondition is only called by the
  1455. // buildTree method. This function assumes that the argument
  1456. // ConditionBuilder has the right format.
  1457. func attrTypeBuildCondition(node exprNode) (exprNode, error) {
  1458. // Create a string with special characters that can be substituted later: $c
  1459. node.fmtExpr = "attribute_type ($c, $c)"
  1460. return node, nil
  1461. }
  1462. // beginsWithBuildCondition is the function to make exprNodes from
  1463. // BeginsWithCond ConditionBuilders. beginsWithBuildCondition is only
  1464. // called by the buildTree method. This function assumes that the argument
  1465. // ConditionBuilder has the right format.
  1466. func beginsWithBuildCondition(node exprNode) (exprNode, error) {
  1467. // Create a string with special characters that can be substituted later: $c
  1468. node.fmtExpr = "begins_with ($c, $c)"
  1469. return node, nil
  1470. }
  1471. // containsBuildCondition is the function to make exprNodes from
  1472. // ContainsCond ConditionBuilders. containsBuildCondition is only
  1473. // called by the buildTree method. This function assumes that the argument
  1474. // ConditionBuilder has the right format.
  1475. func containsBuildCondition(node exprNode) (exprNode, error) {
  1476. // Create a string with special characters that can be substituted later: $c
  1477. node.fmtExpr = "contains ($c, $c)"
  1478. return node, nil
  1479. }
  1480. // buildChildNodes creates the list of the child exprNodes. This avoids
  1481. // duplication of code amongst the various buildTree functions.
  1482. func (cb ConditionBuilder) buildChildNodes() ([]exprNode, error) {
  1483. childNodes := make([]exprNode, 0, len(cb.conditionList)+len(cb.operandList))
  1484. for _, condition := range cb.conditionList {
  1485. node, err := condition.buildTree()
  1486. if err != nil {
  1487. return []exprNode{}, err
  1488. }
  1489. childNodes = append(childNodes, node)
  1490. }
  1491. for _, ope := range cb.operandList {
  1492. operand, err := ope.BuildOperand()
  1493. if err != nil {
  1494. return []exprNode{}, err
  1495. }
  1496. childNodes = append(childNodes, operand.exprNode)
  1497. }
  1498. return childNodes, nil
  1499. }