gitlab.com/evatix-go/core@v1.3.55/cmd/main/payloadTest01.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"gitlab.com/evatix-go/core/coredata/corejson"
     7  	"gitlab.com/evatix-go/core/coredata/corepayload"
     8  	"gitlab.com/evatix-go/core/errcore"
     9  )
    10  
    11  func payloadTest01() {
    12  	line := []byte("some payload")
    13  	sysUser := corepayload.New.User.System(
    14  		"sys-1",
    15  		"system-user-type")
    16  
    17  	regularUser := corepayload.New.User.All(
    18  		false,
    19  		"some user id",
    20  		"regular-2",
    21  		"system-user-type",
    22  		"authToken",
    23  		"passhash")
    24  
    25  	authInfo := corepayload.AuthInfo{
    26  		Identifier:   "authid",
    27  		ActionType:   "actionType",
    28  		ResourceName: "resourceIdentity",
    29  		SessionInfo: &corepayload.SessionInfo{
    30  			Id:          "session id",
    31  			User:        regularUser,
    32  			SessionPath: "sesssion path",
    33  		},
    34  		UserInfo: &corepayload.UserInfo{
    35  			User:       regularUser,
    36  			SystemUser: sysUser,
    37  		},
    38  	}
    39  
    40  	payload, err := corepayload.New.PayloadWrapper.UsingCreateInstruction(
    41  		&corepayload.PayloadCreateInstruction{
    42  			Name:           "name -- as payload",
    43  			Identifier:     "id",
    44  			TaskTypeName:   "task type",
    45  			EntityType:     "entity",
    46  			CategoryName:   "category",
    47  			HasManyRecords: false,
    48  			Payloads:       &line,
    49  			Attributes: &corepayload.Attributes{
    50  				AuthInfo: authInfo.Ptr(),
    51  			},
    52  		})
    53  
    54  	errcore.MustBeEmpty(err)
    55  
    56  	jsResult := payload.JsonPtr()
    57  	println(jsResult.PrettyJsonString())
    58  
    59  	payload2, err := corepayload.New.PayloadWrapper.DeserializeUsingJsonResult(jsResult)
    60  	errcore.HandleErr(err)
    61  
    62  	println(payload2.JsonPtr().PrettyJsonString())
    63  	println(payload2.BytesConverter().SafeCastString())
    64  
    65  	payload3, err3 := corepayload.New.PayloadWrapper.Create(
    66  		"name3",
    67  		"id3", "taskname3", "category3", jsResult.Bytes)
    68  
    69  	errcore.HandleErr(err3)
    70  	println(payload3.JsonPtr().PrettyJsonString())
    71  	println(payload3.DeserializePayloadsToPayloadWrapperMust().JsonPtr().PrettyJsonString())
    72  	pay4, err := payload3.ClonePtr(true)
    73  	errcore.HandleErr(err)
    74  	pay4.Name = "pay 4"
    75  	pay4.Attributes.AddOrUpdateAnyItem(
    76  		"some key",
    77  		payload3.DeserializePayloadsToPayloadWrapperMust().JsonPtr())
    78  	pay4Json := pay4.JsonPtr()
    79  	pay5 := corepayload.PayloadWrapper{}
    80  	pay4Json.DeserializeMust(&pay5)
    81  	println("Pay 5", pay5.PrettyJsonString())
    82  
    83  	newJson := corejson.Result{}
    84  	pay5.Attributes.AnyKeyValuePairs.DeserializeMust(
    85  		"some key",
    86  		&newJson)
    87  
    88  	println("conv JSON", newJson.PrettyJsonString())
    89  	println(payload3.PrettyJsonString())
    90  	sliceOfPayloads := []*corepayload.PayloadWrapper{
    91  		payload,
    92  		payload2,
    93  		payload3,
    94  		pay4,
    95  	}
    96  
    97  	jsonSlice := corejson.Serialize.Apply(sliceOfPayloads)
    98  	jsonSlice.HandleError()
    99  
   100  	newSlice, err := corepayload.New.PayloadWrapper.DeserializeToMany(jsonSlice.Bytes)
   101  	errcore.HandleErr(err)
   102  
   103  	fmt.Println("new slice", newSlice)
   104  
   105  	payloadsSlice2 := corepayload.New.PayloadsCollection.UsingWrappers(
   106  		sliceOfPayloads...)
   107  	fmt.Println("new slice2", payloadsSlice2.PrettyJsonString())
   108  }