gitlab.com/evatix-go/core@v1.3.55/coredata/corejson/newMapResultsCreator.go (about)

     1  package corejson
     2  
     3  type newMapResultsCreator struct{}
     4  
     5  // UnmarshalUsingBytes
     6  //
     7  //  Aka. alias for UnmarshalUsingBytes
     8  //
     9  //  Should be used when MapResults itself is Serialized
    10  //  and save to somewhere and then unmarshal or deserialize
    11  func (it newMapResultsCreator) UnmarshalUsingBytes(
    12  	deserializingBytes []byte,
    13  ) (*MapResults, error) {
    14  	return it.DeserializeUsingBytes(deserializingBytes)
    15  }
    16  
    17  // DeserializeUsingBytes
    18  //
    19  //  Should be used when MapResults itself is Serialized
    20  //  and save to somewhere and then unmarshal or deserialize
    21  func (it newMapResultsCreator) DeserializeUsingBytes(
    22  	deserializingBytes []byte,
    23  ) (*MapResults, error) {
    24  	empty := it.Empty()
    25  
    26  	err := Deserialize.
    27  		UsingBytes(deserializingBytes, empty)
    28  
    29  	if err == nil {
    30  		return empty, nil
    31  	}
    32  
    33  	return nil, err
    34  }
    35  
    36  func (it newMapResultsCreator) DeserializeUsingResult(
    37  	jsonResult *Result,
    38  ) (*MapResults, error) {
    39  	if jsonResult.HasIssuesOrEmpty() {
    40  		return nil, jsonResult.MeaningfulError()
    41  	}
    42  
    43  	empty := it.Empty()
    44  
    45  	err := Deserialize.
    46  		UsingBytes(jsonResult.SafeBytes(), empty)
    47  
    48  	if err == nil {
    49  		return empty, nil
    50  	}
    51  
    52  	return nil, err
    53  }
    54  
    55  func (it newMapResultsCreator) Empty() *MapResults {
    56  	return &MapResults{
    57  		Items: map[string]Result{},
    58  	}
    59  }
    60  
    61  func (it newMapResultsCreator) UsingCap(
    62  	addCapacity int,
    63  ) *MapResults {
    64  	return &MapResults{
    65  		Items: make(
    66  			map[string]Result,
    67  			addCapacity),
    68  	}
    69  }
    70  
    71  func (it newMapResultsCreator) UsingKeyAnyItems(
    72  	addCapacity int,
    73  	keyAnyItems ...KeyAny,
    74  ) *MapResults {
    75  	length := addCapacity + len(keyAnyItems)
    76  
    77  	if length == 0 || len(keyAnyItems) == 0 {
    78  		return it.UsingCap(length)
    79  	}
    80  
    81  	collection := it.UsingCap(length + addCapacity)
    82  
    83  	return collection.
    84  		AddKeyAnyItems(keyAnyItems...)
    85  }
    86  
    87  func (it newMapResultsCreator) UsingMapOptions(
    88  	isClone, isDeepClone bool,
    89  	addCapacity int,
    90  	mapResults map[string]Result,
    91  ) *MapResults {
    92  	if len(mapResults) == 0 {
    93  		return it.UsingCap(
    94  			addCapacity)
    95  	}
    96  
    97  	hasNoChange :=
    98  		addCapacity == 0 &&
    99  			!isClone &&
   100  			!isDeepClone
   101  
   102  	if hasNoChange {
   103  		return &MapResults{
   104  			Items: mapResults,
   105  		}
   106  	}
   107  
   108  	additionalCapacity :=
   109  		len(mapResults) +
   110  			addCapacity
   111  
   112  	finalMapResults := it.UsingCap(
   113  		additionalCapacity)
   114  
   115  	return finalMapResults.AddMapResultsUsingCloneOption(
   116  		isClone,
   117  		isDeepClone,
   118  		mapResults)
   119  }
   120  
   121  func (it newMapResultsCreator) UsingMapPlusCap(
   122  	addCapacity int,
   123  	mapResults map[string]Result,
   124  ) *MapResults {
   125  	if len(mapResults) == 0 {
   126  		return it.UsingCap(
   127  			addCapacity)
   128  	}
   129  
   130  	return it.UsingMapOptions(
   131  		false,
   132  		false,
   133  		addCapacity,
   134  		mapResults)
   135  }
   136  
   137  func (it newMapResultsCreator) UsingMapPlusCapClone(
   138  	addCapacity int,
   139  	mapResults map[string]Result,
   140  ) *MapResults {
   141  	if len(mapResults) == 0 {
   142  		return it.UsingCap(
   143  			addCapacity)
   144  	}
   145  
   146  	return it.UsingMapOptions(
   147  		true,
   148  		false,
   149  		addCapacity,
   150  		mapResults)
   151  }
   152  
   153  func (it newMapResultsCreator) UsingMapPlusCapDeepClone(
   154  	addCapacity int,
   155  	mapResults map[string]Result,
   156  ) *MapResults {
   157  	if len(mapResults) == 0 {
   158  		return it.UsingCap(
   159  			addCapacity)
   160  	}
   161  
   162  	return it.UsingMapOptions(
   163  		true,
   164  		true,
   165  		addCapacity,
   166  		mapResults)
   167  }
   168  
   169  func (it newMapResultsCreator) UsingMap(
   170  	mapResults map[string]Result,
   171  ) *MapResults {
   172  	if len(mapResults) == 0 {
   173  		return it.Empty()
   174  	}
   175  
   176  	return it.UsingMapOptions(
   177  		false,
   178  		false,
   179  		0,
   180  		mapResults)
   181  }
   182  
   183  func (it newMapResultsCreator) UsingMapAnyItemsPlusCap(
   184  	addCapacity int,
   185  	mapAnyItems map[string]interface{},
   186  ) *MapResults {
   187  	if len(mapAnyItems) == 0 {
   188  		return it.UsingCap(addCapacity)
   189  	}
   190  
   191  	collection := it.UsingCap(
   192  		addCapacity + len(mapAnyItems))
   193  
   194  	return collection.
   195  		AddMapAnyItems(mapAnyItems)
   196  }
   197  
   198  func (it newMapResultsCreator) UsingMapAnyItems(
   199  	mapAnyItems map[string]interface{},
   200  ) *MapResults {
   201  	return it.UsingMapAnyItemsPlusCap(
   202  		0,
   203  		mapAnyItems)
   204  }
   205  
   206  func (it newMapResultsCreator) UsingKeyWithResultsPlusCap(
   207  	addCapacity int,
   208  	keyWithResults ...KeyWithResult,
   209  ) *MapResults {
   210  	if keyWithResults == nil {
   211  		return it.UsingCap(addCapacity)
   212  	}
   213  
   214  	mapResults := it.UsingCap(
   215  		addCapacity + len(keyWithResults))
   216  
   217  	return mapResults.
   218  		AddKeysWithResults(keyWithResults...)
   219  }
   220  
   221  func (it newMapResultsCreator) UsingKeyWithResults(
   222  	keyWithResults ...KeyWithResult,
   223  ) *MapResults {
   224  	return it.UsingKeyWithResultsPlusCap(
   225  		0,
   226  		keyWithResults...)
   227  }
   228  
   229  func (it newMapResultsCreator) UsingKeyJsonersPlusCap(
   230  	addCapacity int,
   231  	keyWithJsoners ...KeyWithJsoner,
   232  ) *MapResults {
   233  	if keyWithJsoners == nil {
   234  		return it.UsingCap(addCapacity)
   235  	}
   236  
   237  	mapResults := it.UsingCap(
   238  		addCapacity + len(keyWithJsoners))
   239  
   240  	return mapResults.
   241  		AddKeysWithJsoners(keyWithJsoners...)
   242  }
   243  
   244  func (it newMapResultsCreator) UsingKeyJsoners(
   245  	keyWithJsoners ...KeyWithJsoner,
   246  ) *MapResults {
   247  	return it.UsingKeyJsonersPlusCap(
   248  		0,
   249  		keyWithJsoners...)
   250  }