gitlab.com/evatix-go/core@v1.3.55/coredata/corepayload/newAttributesCreator.go (about)

     1  package corepayload
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/coredata/coredynamic"
     5  	"gitlab.com/evatix-go/core/coredata/corejson"
     6  	"gitlab.com/evatix-go/core/coredata/corestr"
     7  	"gitlab.com/evatix-go/core/coreinstruction"
     8  	"gitlab.com/evatix-go/core/coreinterface/errcoreinf"
     9  	"gitlab.com/evatix-go/core/errcore"
    10  	"gitlab.com/evatix-go/core/isany"
    11  )
    12  
    13  type newAttributesCreator struct{}
    14  
    15  func (it newAttributesCreator) CastOrDeserializeFrom(
    16  	anyItem interface{},
    17  ) (*Attributes, error) {
    18  	if isany.Null(anyItem) {
    19  		return nil, errcore.
    20  			CannotBeNilOrEmptyType.
    21  			ErrorNoRefs(
    22  				"given any item is nil failed to convert to attributes")
    23  	}
    24  
    25  	toAttributes := &Attributes{}
    26  	err := corejson.CastAny.FromToDefault(
    27  		anyItem,
    28  		toAttributes)
    29  
    30  	return toAttributes, err
    31  }
    32  
    33  func (it newAttributesCreator) Deserialize(
    34  	rawBytes []byte,
    35  ) (*Attributes, error) {
    36  	empty := &Attributes{}
    37  	err := corejson.
    38  		Deserialize.
    39  		UsingBytes(rawBytes, empty)
    40  
    41  	if err == nil {
    42  		return empty, nil
    43  	}
    44  
    45  	// has error
    46  	return nil, err
    47  }
    48  
    49  func (it newAttributesCreator) DeserializeMany(
    50  	rawBytes []byte,
    51  ) (attrSlice []*Attributes, err error) {
    52  	err = corejson.
    53  		Deserialize.
    54  		UsingBytes(rawBytes, &attrSlice)
    55  
    56  	if err == nil {
    57  		return attrSlice, nil
    58  	}
    59  
    60  	// has error
    61  	return nil, err
    62  }
    63  
    64  func (it newAttributesCreator) DeserializeUsingJsonResult(
    65  	jsonResult *corejson.Result,
    66  ) (*Attributes, error) {
    67  	empty := &Attributes{}
    68  	err := corejson.
    69  		Deserialize.
    70  		UsingResult(jsonResult, empty)
    71  
    72  	if err == nil {
    73  		return empty, nil
    74  	}
    75  
    76  	// has error
    77  	return nil, err
    78  }
    79  
    80  func (it newAttributesCreator) Create(
    81  	basicErrWrapper errcoreinf.BasicErrWrapper,
    82  	authInfo *AuthInfo,
    83  	dynamicPayloads []byte,
    84  ) *Attributes {
    85  	return &Attributes{
    86  		BasicErrWrapper:  basicErrWrapper,
    87  		AuthInfo:         authInfo,
    88  		KeyValuePairs:    corestr.Empty.Hashmap(),
    89  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
    90  		DynamicPayloads:  dynamicPayloads,
    91  	}
    92  }
    93  
    94  func (it newAttributesCreator) ErrFromTo(
    95  	basicErrWrapper errcoreinf.BasicErrWrapper,
    96  	fromTo *coreinstruction.FromTo,
    97  	dynamicPayloads []byte,
    98  ) *Attributes {
    99  	return &Attributes{
   100  		BasicErrWrapper:  basicErrWrapper,
   101  		KeyValuePairs:    corestr.Empty.Hashmap(),
   102  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   103  		FromTo:           fromTo,
   104  		DynamicPayloads:  dynamicPayloads,
   105  	}
   106  }
   107  
   108  func (it newAttributesCreator) UsingAuthInfoJsonResult(
   109  	authInfo *AuthInfo,
   110  	jsonResult *corejson.Result,
   111  ) (*Attributes, error) {
   112  	return &Attributes{
   113  			AuthInfo:         authInfo,
   114  			KeyValuePairs:    corestr.Empty.Hashmap(),
   115  			AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   116  			DynamicPayloads:  jsonResult.Bytes,
   117  		},
   118  		jsonResult.MeaningfulError()
   119  }
   120  
   121  func (it newAttributesCreator) UsingAuthInfoDynamicBytes(
   122  	authInfo *AuthInfo,
   123  	dynamicPayloads []byte,
   124  ) *Attributes {
   125  	return &Attributes{
   126  		AuthInfo:         authInfo,
   127  		KeyValuePairs:    corestr.Empty.Hashmap(),
   128  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   129  		DynamicPayloads:  dynamicPayloads,
   130  	}
   131  }
   132  
   133  func (it newAttributesCreator) UsingDynamicPayloadBytes(
   134  	dynamicPayloads []byte,
   135  ) *Attributes {
   136  	return &Attributes{
   137  		KeyValuePairs:    corestr.Empty.Hashmap(),
   138  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   139  		DynamicPayloads:  dynamicPayloads,
   140  	}
   141  }
   142  
   143  func (it newAttributesCreator) AllAny(
   144  	authInfo *AuthInfo,
   145  	keyValues *corestr.Hashmap,
   146  	anyKeyValues *coredynamic.MapAnyItems,
   147  	pagingInfo *PagingInfo,
   148  	anyItem interface{},
   149  ) (*Attributes, error) {
   150  	jsonResult := corejson.
   151  		Serialize.
   152  		UsingAny(anyItem)
   153  
   154  	return &Attributes{
   155  		AuthInfo:         authInfo,
   156  		PagingInfo:       pagingInfo,
   157  		KeyValuePairs:    keyValues,
   158  		AnyKeyValuePairs: anyKeyValues,
   159  		DynamicPayloads:  jsonResult.SafeBytes(),
   160  	}, jsonResult.MeaningfulError()
   161  }
   162  
   163  func (it newAttributesCreator) PageInfoAny(
   164  	pagingInfo *PagingInfo,
   165  	anyItem interface{},
   166  ) (*Attributes, error) {
   167  	jsonResult := corejson.
   168  		Serialize.
   169  		UsingAny(anyItem)
   170  
   171  	return &Attributes{
   172  		PagingInfo:       pagingInfo,
   173  		KeyValuePairs:    corestr.Empty.Hashmap(),
   174  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   175  		DynamicPayloads:  jsonResult.SafeBytes(),
   176  	}, jsonResult.MeaningfulError()
   177  }
   178  
   179  func (it newAttributesCreator) All(
   180  	authInfo *AuthInfo,
   181  	keyValues *corestr.Hashmap,
   182  	anyKeyValues *coredynamic.MapAnyItems,
   183  	pagingInfo *PagingInfo,
   184  	dynamicPayloads []byte,
   185  	fromTo *coreinstruction.FromTo,
   186  	basicErr errcoreinf.BasicErrWrapper,
   187  ) *Attributes {
   188  	return &Attributes{
   189  		BasicErrWrapper:  basicErr,
   190  		AuthInfo:         authInfo,
   191  		PagingInfo:       pagingInfo,
   192  		KeyValuePairs:    keyValues,
   193  		AnyKeyValuePairs: anyKeyValues,
   194  		FromTo:           fromTo,
   195  		DynamicPayloads:  dynamicPayloads,
   196  	}
   197  }
   198  
   199  func (it newAttributesCreator) UsingAuthInfo(
   200  	authInfo *AuthInfo,
   201  ) *Attributes {
   202  	return &Attributes{
   203  		AuthInfo:         authInfo,
   204  		KeyValuePairs:    corestr.Empty.Hashmap(),
   205  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   206  	}
   207  }
   208  
   209  func (it newAttributesCreator) UsingDynamicPayloadAny(
   210  	authInfo *AuthInfo,
   211  	anyItem interface{},
   212  ) (*Attributes, error) {
   213  	jsonResult := corejson.
   214  		Serialize.
   215  		UsingAny(anyItem)
   216  
   217  	attr := &Attributes{
   218  		AuthInfo:         authInfo,
   219  		KeyValuePairs:    corestr.Empty.Hashmap(),
   220  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   221  		DynamicPayloads:  jsonResult.SafeBytes(),
   222  	}
   223  
   224  	return attr, jsonResult.MeaningfulError()
   225  }
   226  
   227  func (it newAttributesCreator) UsingKeyValues(
   228  	keyValues *corestr.Hashmap,
   229  ) *Attributes {
   230  	return &Attributes{
   231  		KeyValuePairs:    keyValues,
   232  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   233  		DynamicPayloads:  []byte{},
   234  	}
   235  }
   236  
   237  func (it newAttributesCreator) UsingAuthInfoKeyValues(
   238  	authInfo *AuthInfo,
   239  	keyValues *corestr.Hashmap,
   240  ) *Attributes {
   241  	return &Attributes{
   242  		AuthInfo:         authInfo,
   243  		KeyValuePairs:    keyValues,
   244  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   245  		DynamicPayloads:  []byte{},
   246  	}
   247  }
   248  
   249  func (it newAttributesCreator) UsingKeyValuesPlusDynamic(
   250  	keyValues *corestr.Hashmap,
   251  	dynamicPayloads []byte,
   252  ) *Attributes {
   253  	return &Attributes{
   254  		KeyValuePairs:    keyValues,
   255  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   256  		DynamicPayloads:  dynamicPayloads,
   257  	}
   258  }
   259  
   260  func (it newAttributesCreator) UsingAuthInfoAnyKeyValues(
   261  	authInfo *AuthInfo,
   262  	anyKeyValues *coredynamic.MapAnyItems,
   263  ) *Attributes {
   264  	return &Attributes{
   265  		AuthInfo:         authInfo,
   266  		KeyValuePairs:    corestr.Empty.Hashmap(),
   267  		AnyKeyValuePairs: anyKeyValues,
   268  		DynamicPayloads:  []byte{},
   269  	}
   270  }
   271  
   272  func (it newAttributesCreator) UsingAnyKeyValues(
   273  	anyKeyValues *coredynamic.MapAnyItems,
   274  ) *Attributes {
   275  	return it.UsingAnyKeyValuesPlusDynamic(
   276  		anyKeyValues,
   277  		[]byte{})
   278  }
   279  
   280  func (it newAttributesCreator) UsingAnyKeyValuesPlusDynamic(
   281  	anyKeyValues *coredynamic.MapAnyItems,
   282  	dynamicPayloads []byte,
   283  ) *Attributes {
   284  	return &Attributes{
   285  		KeyValuePairs:    corestr.Empty.Hashmap(),
   286  		AnyKeyValuePairs: anyKeyValues,
   287  		DynamicPayloads:  dynamicPayloads,
   288  	}
   289  }
   290  
   291  func (it newAttributesCreator) UsingBasicError(
   292  	basicErrWrapper errcoreinf.BasicErrWrapper,
   293  ) *Attributes {
   294  	return &Attributes{
   295  		BasicErrWrapper:  basicErrWrapper,
   296  		KeyValuePairs:    corestr.Empty.Hashmap(),
   297  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   298  		DynamicPayloads:  []byte{},
   299  	}
   300  }
   301  
   302  func (it newAttributesCreator) Empty() *Attributes {
   303  	return &Attributes{
   304  		KeyValuePairs:    corestr.Empty.Hashmap(),
   305  		AnyKeyValuePairs: coredynamic.EmptyMapAnyItems(),
   306  		DynamicPayloads:  []byte{},
   307  	}
   308  }