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

     1  package corepayload
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"gitlab.com/evatix-go/core/converters"
     7  	"gitlab.com/evatix-go/core/coredata/corejson"
     8  	"gitlab.com/evatix-go/core/coreinterface/errcoreinf"
     9  	"gitlab.com/evatix-go/core/errcore"
    10  	"gitlab.com/evatix-go/core/internal/reflectinternal"
    11  	"gitlab.com/evatix-go/core/isany"
    12  )
    13  
    14  type newPayloadWrapperCreator struct{}
    15  
    16  func (it newPayloadWrapperCreator) Empty() *PayloadWrapper {
    17  	return &PayloadWrapper{
    18  		Payloads:   []byte{},
    19  		Attributes: New.Attributes.Empty(),
    20  	}
    21  }
    22  
    23  func (it newPayloadWrapperCreator) Deserialize(
    24  	rawBytes []byte,
    25  ) (*PayloadWrapper, error) {
    26  	empty := it.Empty()
    27  
    28  	err := corejson.
    29  		Deserialize.
    30  		UsingBytes(
    31  			rawBytes, empty)
    32  
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	return empty, nil
    38  }
    39  
    40  func (it newPayloadWrapperCreator) CastOrDeserializeFrom(
    41  	anyItem interface{},
    42  ) (*PayloadWrapper, error) {
    43  	if isany.Null(anyItem) {
    44  		return nil, errcore.
    45  			CannotBeNilOrEmptyType.
    46  			ErrorNoRefs(
    47  				"given any item is nil failed to convert to payload-wrapper")
    48  	}
    49  
    50  	toPayloadWrapper := &PayloadWrapper{}
    51  	err := corejson.CastAny.FromToDefault(
    52  		anyItem,
    53  		toPayloadWrapper)
    54  
    55  	return toPayloadWrapper, err
    56  }
    57  
    58  func (it newPayloadWrapperCreator) DeserializeToMany(
    59  	rawBytes []byte,
    60  ) (payloadsSlice []*PayloadWrapper, err error) {
    61  	err = corejson.
    62  		Deserialize.
    63  		UsingBytes(
    64  			rawBytes,
    65  			&payloadsSlice)
    66  
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	return payloadsSlice, nil
    72  }
    73  
    74  func (it newPayloadWrapperCreator) DeserializeToCollection(
    75  	rawBytes []byte,
    76  ) (payloadsSlice *PayloadsCollection, err error) {
    77  	return New.
    78  		PayloadsCollection.
    79  		Deserialize(
    80  			rawBytes)
    81  }
    82  
    83  func (it newPayloadWrapperCreator) DeserializeUsingJsonResult(
    84  	jsonResult *corejson.Result,
    85  ) (*PayloadWrapper, error) {
    86  	empty := it.Empty()
    87  
    88  	err := corejson.
    89  		Deserialize.
    90  		Apply(jsonResult, empty)
    91  
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	return empty, nil
    97  }
    98  
    99  func (it newPayloadWrapperCreator) UsingBytesCreateInstructionTypeStringer(
   100  	createInstruction *BytesCreateInstructionStringer,
   101  ) *PayloadWrapper {
   102  	return it.createInternalUsingBytes(
   103  		createInstruction.Name,
   104  		createInstruction.Identifier,
   105  		createInstruction.TaskTypeName.String(),
   106  		createInstruction.CategoryName.String(),
   107  		createInstruction.EntityType,
   108  		createInstruction.HasManyRecords,
   109  		createInstruction.Payloads,
   110  		createInstruction.Attributes,
   111  		nil)
   112  }
   113  
   114  func (it newPayloadWrapperCreator) UsingBytesCreateInstruction(
   115  	createInstruction *BytesCreateInstruction,
   116  ) *PayloadWrapper {
   117  	return it.createInternalUsingBytes(
   118  		createInstruction.Name,
   119  		createInstruction.Identifier,
   120  		createInstruction.TaskTypeName,
   121  		createInstruction.CategoryName,
   122  		createInstruction.EntityType,
   123  		createInstruction.HasManyRecords,
   124  		createInstruction.Payloads,
   125  		createInstruction.Attributes,
   126  		nil)
   127  }
   128  
   129  func (it newPayloadWrapperCreator) UsingCreateInstructionTypeStringer(
   130  	createInstruction *PayloadCreateInstructionTypeStringer,
   131  ) (*PayloadWrapper, error) {
   132  	return it.UsingCreateInstruction(createInstruction.PayloadCreateInstruction())
   133  }
   134  
   135  func (it newPayloadWrapperCreator) UsingCreateInstruction(
   136  	createInstruction *PayloadCreateInstruction,
   137  ) (*PayloadWrapper, error) {
   138  	switch payloadCasted := createInstruction.Payloads.(type) {
   139  	case []byte:
   140  		return it.createInternalUsingBytes(
   141  			createInstruction.Name,
   142  			createInstruction.Identifier,
   143  			createInstruction.TaskTypeName,
   144  			createInstruction.CategoryName,
   145  			createInstruction.EntityType,
   146  			createInstruction.HasManyRecords,
   147  			payloadCasted,
   148  			createInstruction.Attributes,
   149  			nil), nil
   150  	case *[]byte:
   151  		return it.createInternalUsingBytes(
   152  			createInstruction.Name,
   153  			createInstruction.Identifier,
   154  			createInstruction.TaskTypeName,
   155  			createInstruction.CategoryName,
   156  			createInstruction.EntityType,
   157  			createInstruction.HasManyRecords,
   158  			converters.BytesPointerToBytes(payloadCasted),
   159  			createInstruction.Attributes,
   160  			nil), nil
   161  	case string:
   162  		return it.createInternalUsingBytes(
   163  			createInstruction.Name,
   164  			createInstruction.Identifier,
   165  			createInstruction.TaskTypeName,
   166  			createInstruction.CategoryName,
   167  			createInstruction.EntityType,
   168  			createInstruction.HasManyRecords,
   169  			[]byte(payloadCasted),
   170  			createInstruction.Attributes,
   171  			nil), nil
   172  	default: // any
   173  		return it.createInternal(
   174  			createInstruction.Name,
   175  			createInstruction.Identifier,
   176  			createInstruction.TaskTypeName,
   177  			createInstruction.CategoryName,
   178  			createInstruction.HasManyRecords,
   179  			payloadCasted, // any
   180  			createInstruction.Attributes,
   181  		)
   182  	}
   183  }
   184  
   185  func (it newPayloadWrapperCreator) UsingBytes(
   186  	name, id, taskName,
   187  	category, entityName string,
   188  	payload []byte,
   189  ) *PayloadWrapper {
   190  	payloadWrapper, err := it.UsingCreateInstruction(
   191  		&PayloadCreateInstruction{
   192  			Name:         name,
   193  			Identifier:   id,
   194  			TaskTypeName: taskName,
   195  			EntityType:   entityName,
   196  			CategoryName: category,
   197  			Payloads:     payload,
   198  		})
   199  
   200  	errcore.MustBeEmpty(err)
   201  
   202  	return payloadWrapper
   203  }
   204  
   205  func (it newPayloadWrapperCreator) Create(
   206  	name, id, taskName, category string,
   207  	record interface{},
   208  ) (*PayloadWrapper, error) {
   209  	return it.UsingCreateInstruction(
   210  		&PayloadCreateInstruction{
   211  			Name:         name,
   212  			Identifier:   id,
   213  			TaskTypeName: taskName,
   214  			EntityType:   reflectinternal.SafeTypeName(record),
   215  			CategoryName: category,
   216  			Payloads:     record,
   217  		})
   218  }
   219  
   220  func (it newPayloadWrapperCreator) CreateUsingTypeStringer(
   221  	name, id string,
   222  	taskNameStringer, categoryStringer fmt.Stringer,
   223  	record interface{},
   224  ) (*PayloadWrapper, error) {
   225  	return it.UsingCreateInstruction(
   226  		&PayloadCreateInstruction{
   227  			Name:         name,
   228  			Identifier:   id,
   229  			TaskTypeName: taskNameStringer.String(),
   230  			EntityType:   reflectinternal.SafeTypeName(record),
   231  			CategoryName: categoryStringer.String(),
   232  			Payloads:     record,
   233  		})
   234  }
   235  
   236  func (it newPayloadWrapperCreator) NameIdCategory(
   237  	name, id, category string,
   238  	record interface{},
   239  ) (*PayloadWrapper, error) {
   240  	entity := reflectinternal.SafeTypeName(
   241  		record)
   242  
   243  	return it.UsingCreateInstruction(
   244  		&PayloadCreateInstruction{
   245  			Name:         name,
   246  			Identifier:   id,
   247  			TaskTypeName: entity,
   248  			EntityType:   entity,
   249  			CategoryName: category,
   250  			Payloads:     record,
   251  		})
   252  }
   253  
   254  func (it newPayloadWrapperCreator) NameIdCategoryStringer(
   255  	name, id string,
   256  	categoryStringer fmt.Stringer,
   257  	record interface{},
   258  ) (*PayloadWrapper, error) {
   259  	entity := reflectinternal.SafeTypeName(
   260  		record)
   261  
   262  	return it.UsingCreateInstruction(
   263  		&PayloadCreateInstruction{
   264  			Name:         name,
   265  			Identifier:   id,
   266  			TaskTypeName: entity,
   267  			EntityType:   entity,
   268  			CategoryName: categoryStringer.String(),
   269  			Payloads:     record,
   270  		})
   271  }
   272  
   273  func (it newPayloadWrapperCreator) Records(
   274  	name, id, taskName, category string,
   275  	records interface{},
   276  ) (*PayloadWrapper, error) {
   277  	return it.UsingCreateInstruction(
   278  		&PayloadCreateInstruction{
   279  			Name:         name,
   280  			Identifier:   id,
   281  			TaskTypeName: taskName,
   282  			EntityType: reflectinternal.SafeTypeNameOfSliceOrSingle(
   283  				false, records),
   284  			CategoryName:   category,
   285  			HasManyRecords: true,
   286  			Payloads:       records,
   287  		})
   288  }
   289  
   290  func (it newPayloadWrapperCreator) RecordsTypeStringer(
   291  	name, id string,
   292  	taskNameStringer, categoryStringer fmt.Stringer,
   293  	records interface{},
   294  ) (*PayloadWrapper, error) {
   295  	return it.UsingCreateInstruction(
   296  		&PayloadCreateInstruction{
   297  			Name:         name,
   298  			Identifier:   id,
   299  			TaskTypeName: taskNameStringer.String(),
   300  			EntityType: reflectinternal.SafeTypeNameOfSliceOrSingle(
   301  				false, records),
   302  			CategoryName:   categoryStringer.String(),
   303  			HasManyRecords: true,
   304  			Payloads:       records,
   305  		})
   306  }
   307  
   308  func (it newPayloadWrapperCreator) Record(
   309  	name, id, taskName, category string,
   310  	record interface{},
   311  ) (*PayloadWrapper, error) {
   312  	return it.UsingCreateInstruction(
   313  		&PayloadCreateInstruction{
   314  			Name:         name,
   315  			Identifier:   id,
   316  			TaskTypeName: taskName,
   317  			EntityType: reflectinternal.SafeTypeName(
   318  				record),
   319  			CategoryName: category,
   320  			Payloads:     record,
   321  		})
   322  }
   323  
   324  func (it newPayloadWrapperCreator) RecordTypeStringer(
   325  	name, id string,
   326  	taskNameStringer, categoryStringer fmt.Stringer,
   327  	record interface{},
   328  ) (*PayloadWrapper, error) {
   329  	return it.UsingCreateInstruction(
   330  		&PayloadCreateInstruction{
   331  			Name:         name,
   332  			Identifier:   id,
   333  			TaskTypeName: taskNameStringer.String(),
   334  			EntityType: reflectinternal.SafeTypeName(
   335  				record),
   336  			CategoryName: categoryStringer.String(),
   337  			Payloads:     record,
   338  		})
   339  }
   340  
   341  func (it newPayloadWrapperCreator) NameIdTaskRecord(
   342  	name, id, taskName string,
   343  	record interface{},
   344  ) (*PayloadWrapper, error) {
   345  	return it.UsingCreateInstruction(
   346  		&PayloadCreateInstruction{
   347  			Name:         name,
   348  			Identifier:   id,
   349  			TaskTypeName: taskName,
   350  			EntityType:   reflectinternal.SafeTypeName(record),
   351  			Payloads:     record,
   352  		})
   353  }
   354  
   355  func (it newPayloadWrapperCreator) NameIdTaskStringerRecord(
   356  	name, id string,
   357  	taskNameStringer fmt.Stringer,
   358  	record interface{},
   359  ) (*PayloadWrapper, error) {
   360  	return it.UsingCreateInstruction(
   361  		&PayloadCreateInstruction{
   362  			Name:         name,
   363  			Identifier:   id,
   364  			TaskTypeName: taskNameStringer.String(),
   365  			EntityType:   reflectinternal.SafeTypeName(record),
   366  			Payloads:     record,
   367  		})
   368  }
   369  
   370  func (it newPayloadWrapperCreator) NameIdRecord(
   371  	name, id string,
   372  	record interface{},
   373  ) (*PayloadWrapper, error) {
   374  	return it.UsingCreateInstruction(
   375  		&PayloadCreateInstruction{
   376  			Name:       name,
   377  			Identifier: id,
   378  			EntityType: reflectinternal.SafeTypeName(record),
   379  			Payloads:   record,
   380  		})
   381  }
   382  
   383  func (it newPayloadWrapperCreator) NameTaskNameRecord(
   384  	id, taskName string,
   385  	record interface{},
   386  ) (*PayloadWrapper, error) {
   387  	return it.UsingCreateInstruction(
   388  		&PayloadCreateInstruction{
   389  			Identifier:   id,
   390  			TaskTypeName: taskName,
   391  			EntityType:   reflectinternal.SafeTypeName(record),
   392  			Payloads:     record,
   393  		})
   394  }
   395  
   396  func (it newPayloadWrapperCreator) createInternal(
   397  	name, id, taskName, category string,
   398  	hasManyRecords bool,
   399  	records interface{},
   400  	attr *Attributes,
   401  ) (*PayloadWrapper, error) {
   402  	jsonResult := corejson.
   403  		Serialize.
   404  		UsingAny(records)
   405  
   406  	entityTypeName := reflectinternal.SafeTypeNameOfSliceOrSingle(
   407  		!hasManyRecords,
   408  		records)
   409  
   410  	return &PayloadWrapper{
   411  		Name:           name,
   412  		Identifier:     id,
   413  		TaskTypeName:   taskName,
   414  		HasManyRecords: hasManyRecords,
   415  		EntityType:     entityTypeName,
   416  		CategoryName:   category,
   417  		Payloads:       jsonResult.SafeBytes(),
   418  		Attributes:     attr,
   419  	}, jsonResult.MeaningfulError()
   420  }
   421  
   422  func (it newPayloadWrapperCreator) createInternalUsingBytes(
   423  	name, id, taskName,
   424  	category, entityName string,
   425  	hasManyRecords bool,
   426  	payloads []byte,
   427  	attr *Attributes,
   428  	basicErr errcoreinf.BasicErrWrapper, // will be mutated inside the attr error
   429  ) *PayloadWrapper {
   430  	if attr == nil {
   431  		attr = New.Attributes.UsingBasicError(basicErr)
   432  	} else {
   433  		attr.SetBasicErr(basicErr)
   434  	}
   435  
   436  	return &PayloadWrapper{
   437  		Name:           name,
   438  		Identifier:     id,
   439  		TaskTypeName:   taskName,
   440  		EntityType:     entityName,
   441  		HasManyRecords: hasManyRecords,
   442  		CategoryName:   category,
   443  		Payloads:       payloads,
   444  		Attributes:     attr,
   445  	}
   446  }
   447  
   448  func (it newPayloadWrapperCreator) ManyRecords(
   449  	name, id, taskName, category string,
   450  	records interface{},
   451  ) (*PayloadWrapper, error) {
   452  	jsonResult := corejson.
   453  		Serialize.
   454  		UsingAny(records)
   455  
   456  	return &PayloadWrapper{
   457  		Name:         name,
   458  		Identifier:   id,
   459  		TaskTypeName: taskName,
   460  		EntityType:   reflectinternal.SafeTypeName(records),
   461  		CategoryName: category,
   462  		Payloads:     jsonResult.SafeBytes(),
   463  	}, jsonResult.MeaningfulError()
   464  }
   465  
   466  func (it newPayloadWrapperCreator) All(
   467  	name, id, taskName,
   468  	category, entityTypeName string,
   469  	hasManyRecords bool,
   470  	attributes *Attributes,
   471  	payloads []byte,
   472  ) *PayloadWrapper {
   473  	return &PayloadWrapper{
   474  		Name:           name,
   475  		Identifier:     id,
   476  		TaskTypeName:   taskName,
   477  		EntityType:     entityTypeName,
   478  		CategoryName:   category,
   479  		HasManyRecords: hasManyRecords,
   480  		Payloads:       payloads,
   481  		Attributes:     attributes,
   482  	}
   483  }
   484  
   485  func (it newPayloadWrapperCreator) AllUsingStringer(
   486  	name, id string,
   487  	taskNameStringer,
   488  	categoryStringer fmt.Stringer,
   489  	entityTypeName string,
   490  	hasManyRecords bool,
   491  	attributes *Attributes,
   492  	payloads []byte,
   493  ) *PayloadWrapper {
   494  	return &PayloadWrapper{
   495  		Name:           name,
   496  		Identifier:     id,
   497  		TaskTypeName:   taskNameStringer.String(),
   498  		EntityType:     entityTypeName,
   499  		CategoryName:   categoryStringer.String(),
   500  		HasManyRecords: hasManyRecords,
   501  		Payloads:       payloads,
   502  		Attributes:     attributes,
   503  	}
   504  }
   505  
   506  func (it newPayloadWrapperCreator) AllUsingExpander(
   507  	name, id string,
   508  	typeExpander PayloadTypeExpander,
   509  	entityTypeName string,
   510  	hasManyRecords bool,
   511  	attributes *Attributes,
   512  	payloads []byte,
   513  ) *PayloadWrapper {
   514  	return &PayloadWrapper{
   515  		Name:           name,
   516  		Identifier:     id,
   517  		TaskTypeName:   typeExpander.TaskTypeStringer.String(),
   518  		EntityType:     entityTypeName,
   519  		CategoryName:   typeExpander.CategoryStringer.String(),
   520  		HasManyRecords: hasManyRecords,
   521  		Payloads:       payloads,
   522  		Attributes:     attributes,
   523  	}
   524  }