code.dwrz.net

Go monorepo.
Log | Files | Refs

array.go (2044B)


      1 package query
      2 
      3 import (
      4 	"fmt"
      5 	"net/url"
      6 )
      7 
      8 // Array represents the encoding of Query lists and sets. A Query array is a
      9 // representation of a list of values of a fixed type. A serialized array might
     10 // look like the following:
     11 //
     12 //	ListName.member.1=foo
     13 //	&ListName.member.2=bar
     14 //	&Listname.member.3=baz
     15 type Array struct {
     16 	// The query values to add the array to.
     17 	values url.Values
     18 	// The array's prefix, which includes the names of all parent structures
     19 	// and ends with the name of the list. For example, the prefix might be
     20 	// "ParentStructure.ListName". This prefix will be used to form the full
     21 	// keys for each element in the list. For example, an entry might have the
     22 	// key "ParentStructure.ListName.member.MemberName.1".
     23 	//
     24 	// While this is currently represented as a string that gets added to, it
     25 	// could also be represented as a stack that only gets condensed into a
     26 	// string when a finalized key is created. This could potentially reduce
     27 	// allocations.
     28 	prefix string
     29 	// Whether the list is flat or not. A list that is not flat will produce the
     30 	// following entry to the url.Values for a given entry:
     31 	//     ListName.MemberName.1=value
     32 	// A list that is flat will produce the following:
     33 	//     ListName.1=value
     34 	flat bool
     35 	// The location name of the member. In most cases this should be "member".
     36 	memberName string
     37 	// Elements are stored in values, so we keep track of the list size here.
     38 	size int32
     39 }
     40 
     41 func newArray(values url.Values, prefix string, flat bool, memberName string) *Array {
     42 	return &Array{
     43 		values:     values,
     44 		prefix:     prefix,
     45 		flat:       flat,
     46 		memberName: memberName,
     47 	}
     48 }
     49 
     50 // Value adds a new element to the Query Array. Returns a Value type used to
     51 // encode the array element.
     52 func (a *Array) Value() Value {
     53 	// Query lists start a 1, so adjust the size first
     54 	a.size++
     55 	prefix := a.prefix
     56 	if !a.flat {
     57 		prefix = fmt.Sprintf("%s.%s", prefix, a.memberName)
     58 	}
     59 	// Lists can't have flat members
     60 	return newValue(a.values, fmt.Sprintf("%s.%d", prefix, a.size), false)
     61 }