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

     1  package expression
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // ProjectionBuilder represents Projection Expressions in DynamoDB.
     8  // ProjectionBuilders are the building blocks of Builders.
     9  // More Information at: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ProjectionExpressions.html
    10  type ProjectionBuilder struct {
    11  	names []NameBuilder
    12  }
    13  
    14  // NamesList returns a ProjectionBuilder representing the list of item
    15  // attribute names specified by the argument NameBuilders. The resulting
    16  // ProjectionBuilder can be used as a part of other ProjectionBuilders or as an
    17  // argument to the WithProjection() method for the Builder struct.
    18  //
    19  // Example:
    20  //
    21  //     // projection represents the list of names {"foo", "bar"}
    22  //     projection := expression.NamesList(expression.Name("foo"), expression.Name("bar"))
    23  //
    24  //     // Used in another Projection Expression
    25  //     anotherProjection := expression.AddNames(projection, expression.Name("baz"))
    26  //
    27  //     // Used to make an Builder
    28  //     builder := expression.NewBuilder().WithProjection(anotherProjection)
    29  //
    30  // Expression Equivalent:
    31  //
    32  //     expression.NamesList(expression.Name("foo"), expression.Name("bar"))
    33  //     "foo, bar"
    34  func NamesList(nameBuilder NameBuilder, namesList ...NameBuilder) ProjectionBuilder {
    35  	namesList = append([]NameBuilder{nameBuilder}, namesList...)
    36  	return ProjectionBuilder{
    37  		names: namesList,
    38  	}
    39  }
    40  
    41  // NamesList returns a ProjectionBuilder representing the list of item
    42  // attribute names specified by the argument NameBuilders. The resulting
    43  // ProjectionBuilder can be used as a part of other ProjectionBuilders or as an
    44  // argument to the WithProjection() method for the Builder struct.
    45  //
    46  // Example:
    47  //
    48  //     // projection represents the list of names {"foo", "bar"}
    49  //     projection := expression.Name("foo").NamesList(expression.Name("bar"))
    50  //
    51  //     // Used in another Projection Expression
    52  //     anotherProjection := expression.AddNames(projection, expression.Name("baz"))
    53  //     // Used to make an Builder
    54  //     builder := expression.NewBuilder().WithProjection(newProjection)
    55  //
    56  // Expression Equivalent:
    57  //
    58  //     expression.Name("foo").NamesList(expression.Name("bar"))
    59  //     "foo, bar"
    60  func (nb NameBuilder) NamesList(namesList ...NameBuilder) ProjectionBuilder {
    61  	return NamesList(nb, namesList...)
    62  }
    63  
    64  // AddNames returns a ProjectionBuilder representing the list of item
    65  // attribute names equivalent to appending all of the argument item attribute
    66  // names to the argument ProjectionBuilder. The resulting ProjectionBuilder can
    67  // be used as a part of other ProjectionBuilders or as an argument to the
    68  // WithProjection() method for the Builder struct.
    69  //
    70  // Example:
    71  //
    72  //     // projection represents the list of names {"foo", "bar", "baz", "qux"}
    73  //     oldProj := expression.NamesList(expression.Name("foo"), expression.Name("bar"))
    74  //     projection := expression.AddNames(oldProj, expression.Name("baz"), expression.Name("qux"))
    75  //
    76  //     // Used in another Projection Expression
    77  //     anotherProjection := expression.AddNames(projection, expression.Name("quux"))
    78  //     // Used to make an Builder
    79  //     builder := expression.NewBuilder().WithProjection(newProjection)
    80  //
    81  // Expression Equivalent:
    82  //
    83  //     expression.AddNames(expression.NamesList(expression.Name("foo"), expression.Name("bar")), expression.Name("baz"), expression.Name("qux"))
    84  //     "foo, bar, baz, qux"
    85  func AddNames(projectionBuilder ProjectionBuilder, namesList ...NameBuilder) ProjectionBuilder {
    86  	projectionBuilder.names = append(projectionBuilder.names, namesList...)
    87  	return projectionBuilder
    88  }
    89  
    90  // AddNames returns a ProjectionBuilder representing the list of item
    91  // attribute names equivalent to appending all of the argument item attribute
    92  // names to the argument ProjectionBuilder. The resulting ProjectionBuilder can
    93  // be used as a part of other ProjectionBuilders or as an argument to the
    94  // WithProjection() method for the Builder struct.
    95  //
    96  // Example:
    97  //
    98  //     // projection represents the list of names {"foo", "bar", "baz", "qux"}
    99  //     oldProj := expression.NamesList(expression.Name("foo"), expression.Name("bar"))
   100  //     projection := oldProj.AddNames(expression.Name("baz"), expression.Name("qux"))
   101  //
   102  //     // Used in another Projection Expression
   103  //     anotherProjection := expression.AddNames(projection, expression.Name("quux"))
   104  //     // Used to make an Builder
   105  //     builder := expression.NewBuilder().WithProjection(newProjection)
   106  //
   107  // Expression Equivalent:
   108  //
   109  //     expression.NamesList(expression.Name("foo"), expression.Name("bar")).AddNames(expression.Name("baz"), expression.Name("qux"))
   110  //     "foo, bar, baz, qux"
   111  func (pb ProjectionBuilder) AddNames(namesList ...NameBuilder) ProjectionBuilder {
   112  	return AddNames(pb, namesList...)
   113  }
   114  
   115  // buildTree builds a tree structure of exprNodes based on the tree
   116  // structure of the input ProjectionBuilder's child NameBuilders. buildTree()
   117  // satisfies the treeBuilder interface so ProjectionBuilder can be a part of
   118  // Builder and Expression struct.
   119  func (pb ProjectionBuilder) buildTree() (exprNode, error) {
   120  	if len(pb.names) == 0 {
   121  		return exprNode{}, newUnsetParameterError("buildTree", "ProjectionBuilder")
   122  	}
   123  
   124  	childNodes, err := pb.buildChildNodes()
   125  	if err != nil {
   126  		return exprNode{}, err
   127  	}
   128  	ret := exprNode{
   129  		children: childNodes,
   130  	}
   131  
   132  	ret.fmtExpr = "$c" + strings.Repeat(", $c", len(pb.names)-1)
   133  
   134  	return ret, nil
   135  }
   136  
   137  // buildChildNodes creates the list of the child exprNodes.
   138  func (pb ProjectionBuilder) buildChildNodes() ([]exprNode, error) {
   139  	childNodes := make([]exprNode, 0, len(pb.names))
   140  	for _, name := range pb.names {
   141  		operand, err := name.BuildOperand()
   142  		if err != nil {
   143  			return []exprNode{}, err
   144  		}
   145  		childNodes = append(childNodes, operand.exprNode)
   146  	}
   147  
   148  	return childNodes, nil
   149  }