github.com/altipla-consulting/ravendb-go-client@v0.1.3/index_definition.go (about)

     1  package ravendb
     2  
     3  type IndexDefinition struct {
     4  	Name              string                        `json:"Name"`
     5  	Priority          IndexPriority                 `json:"Priority,omitempty"`
     6  	LockMode          IndexLockMode                 `json:"LockMode,omitempty"`
     7  	AdditionalSources map[string]string             `json:"AdditionalSources"`
     8  	Maps              []string                      `json:"Maps"`
     9  	Reduce            *string                       `json:"Reduce"`
    10  	Fields            map[string]*IndexFieldOptions `json:"Fields"`
    11  	Configuration     IndexConfiguration            `json:"Configuration"`
    12  	IndexType         IndexType                     `json:"Type"`
    13  	//TBD 4.1  bool testIndex;
    14  	OutputReduceToCollection *string `json:"OutputReduceToCollection"`
    15  }
    16  
    17  func toStrPtr(s string) *string {
    18  	if s == "" {
    19  		return nil
    20  	}
    21  	return &s
    22  }
    23  
    24  func NewIndexDefinition() *IndexDefinition {
    25  	res := &IndexDefinition{
    26  		Configuration: NewIndexConfiguration(),
    27  
    28  		// Note: initializing those is possibly wasteful but
    29  		// it's needed to serialize them like Java, as {} and not null
    30  		Fields:            make(map[string]*IndexFieldOptions),
    31  		AdditionalSources: make(map[string]string),
    32  	}
    33  	return res
    34  }
    35  
    36  func (d *IndexDefinition) GetAdditionalSources() map[string]string {
    37  	if d.AdditionalSources == nil {
    38  		d.AdditionalSources = make(map[string]string)
    39  	}
    40  	return d.AdditionalSources
    41  }
    42  
    43  func (d *IndexDefinition) SetAdditionalSources(additionalSources map[string]string) {
    44  	// preserve additionalSources being always non-nil
    45  	// so that JSON serializes it as {} and not 'null'
    46  	if additionalSources == nil {
    47  		if len(d.AdditionalSources) == 0 {
    48  			return
    49  		}
    50  		additionalSources = make(map[string]string)
    51  	}
    52  	d.AdditionalSources = additionalSources
    53  }
    54  
    55  func (d *IndexDefinition) String() string {
    56  	return d.Name
    57  }
    58  
    59  func (d *IndexDefinition) GetFields() map[string]*IndexFieldOptions {
    60  	if d.Fields == nil {
    61  		d.Fields = make(map[string]*IndexFieldOptions)
    62  	}
    63  	return d.Fields
    64  }
    65  
    66  func (d *IndexDefinition) GetConfiguration() IndexConfiguration {
    67  	if d.Configuration == nil {
    68  		d.Configuration = NewIndexConfiguration()
    69  	}
    70  	return d.Configuration
    71  }
    72  
    73  func (d *IndexDefinition) SetConfiguration(configuration IndexConfiguration) {
    74  	d.Configuration = configuration
    75  }
    76  
    77  // Note: this must be called after finishing building index definition to set IndexType
    78  // In Java it's calculated on demand via getType
    79  func (d *IndexDefinition) updateIndexTypeAndMaps() {
    80  	if d.IndexType == "" || d.IndexType == IndexTypeNone {
    81  		d.IndexType = d.detectStaticIndexType()
    82  	}
    83  	d.Maps = stringArrayRemoveDuplicates(d.Maps)
    84  }
    85  
    86  func (d *IndexDefinition) GetType() IndexType {
    87  	if d.IndexType == "" || d.IndexType == IndexTypeNone {
    88  		d.IndexType = d.detectStaticIndexType()
    89  	}
    90  
    91  	return d.IndexType
    92  }
    93  
    94  func (d *IndexDefinition) SetType(indexType IndexType) {
    95  	if indexType == "" {
    96  		indexType = IndexTypeNone
    97  	}
    98  	d.IndexType = indexType
    99  }
   100  
   101  func (d *IndexDefinition) detectStaticIndexType() IndexType {
   102  	if d.Reduce == nil || stringIsBlank(*d.Reduce) {
   103  		return IndexTypeMap
   104  	}
   105  	return IndexTypeMapReduce
   106  }
   107  
   108  //TBD 4.1  bool isTestIndex()
   109  
   110  //TBD 4.1   setTestIndex(bool testIndex)
   111  
   112  func (d *IndexDefinition) GetOutputReduceToCollection() *string {
   113  	return d.OutputReduceToCollection
   114  }
   115  
   116  func (d *IndexDefinition) SetOutputReduceToCollection(outputReduceToCollection string) {
   117  	d.OutputReduceToCollection = toStrPtr(outputReduceToCollection)
   118  }