github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/object/service-get-sdk.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package object
     4  
     5  import (
     6  	"../protocol"
     7  	"../service"
     8  	"../srpc"
     9  	"../syllab"
    10  )
    11  
    12  var GetService = getService{
    13  	Service: service.New("urn:giti:object.protocol:service:get", "", protocol.ServiceStatePreAlpha, 1587282740).
    14  		SetDetail(protocol.LanguageEnglish, "Get",
    15  			`use to get an object by the objectID and structureID! In multi node application, Request must send to proper node otherwise get not found error!`,
    16  			[]string{}).
    17  		SetAuthorization(protocol.CRUDRead, protocol.UserTypeApp).Expired(0, ""),
    18  }
    19  
    20  type getService struct {
    21  	service.Service
    22  }
    23  
    24  func (ser *getService) DoSRPC(req GetRequest) (res protocol.Object, err protocol.Error) {
    25  	var node protocol.ApplicationNode
    26  	node, err = protocol.App.GetNodeByObjectID(req.objectID)
    27  	if err != nil {
    28  		return
    29  	}
    30  
    31  	if node.Status() == protocol.ApplicationStateLocalNode {
    32  		return get(&req)
    33  	}
    34  
    35  	var srpcRes srpc.Response
    36  	srpcRes, err = srpc.HandleOutcomeRequest(node.Conn(), ser, &req)
    37  	if err != nil {
    38  		return
    39  	}
    40  	res = Object(syllab.GetByteArray(srpcRes.Payload(), 0))
    41  	return
    42  }
    43  
    44  /*
    45  	Service request and response shape
    46  */
    47  
    48  type getRequest interface {
    49  	ObjectID() [32]byte
    50  	ObjectStructureID() uint64
    51  }
    52  
    53  /*
    54  	Service Request
    55  */
    56  
    57  // GetRequest is request structure of Get()
    58  type GetRequest struct {
    59  	objectID          [32]byte
    60  	objectStructureID uint64
    61  }
    62  
    63  // methods to implements getRequest interface
    64  func (req *GetRequest) ObjectID() [32]byte               { return req.objectID }
    65  func (req *GetRequest) ObjectStructureID() uint64        { return req.objectStructureID }
    66  func (req *GetRequest) SetObjectID(oID [32]byte)         { req.objectID = oID }
    67  func (req *GetRequest) SetObjectStructureID(osID uint64) { req.objectStructureID = osID }
    68  
    69  // methods to implements protocol.Syllab interface
    70  func (req *GetRequest) CheckSyllab(payload []byte) (err protocol.Error) {
    71  	if len(payload) < int(req.LenOfSyllabStack()) {
    72  		err = syllab.ErrShortArrayDecode
    73  	}
    74  	return
    75  }
    76  func (req *GetRequest) FromSyllab(payload []byte, stackIndex uint32) {
    77  	copy(req.objectID[:], payload[:])
    78  	req.objectStructureID = syllab.GetUInt64(payload, 32)
    79  }
    80  func (req *GetRequest) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
    81  	copy(payload[4:], req.objectID[:])
    82  	syllab.SetUInt64(payload, 36, req.objectStructureID)
    83  	return heapIndex
    84  }
    85  func (req *GetRequest) LenAsSyllab() uint64          { return 40 }
    86  func (req *GetRequest) LenOfSyllabStack() uint32     { return 40 }
    87  func (req *GetRequest) LenOfSyllabHeap() (ln uint32) { return }
    88  
    89  type getRequestSyllab []byte
    90  
    91  // methods to implements getRequest interface
    92  func (req getRequestSyllab) ObjectID() (objectID [32]byte)    { copy(objectID[:], req[0:]); return }
    93  func (req getRequestSyllab) ObjectStructureID() (osID uint64) { return syllab.GetUInt64(req, 32) }
    94  
    95  // methods to implements protocol.Syllab interface
    96  func (req getRequestSyllab) CheckSyllab(payload []byte) (err protocol.Error) {
    97  	if len(req) < int(req.LenOfSyllabStack()) {
    98  		err = syllab.ErrShortArrayDecode
    99  	}
   100  	return
   101  }
   102  func (req getRequestSyllab) FromSyllab(payload []byte, stackIndex uint32) {
   103  	// err = ErrSourceNotChangeable
   104  }
   105  func (req getRequestSyllab) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) {
   106  	copy(payload[stackIndex:], req)
   107  	return heapIndex
   108  }
   109  func (req getRequestSyllab) LenAsSyllab() uint64          { return 40 }
   110  func (req getRequestSyllab) LenOfSyllabStack() uint32     { return 40 }
   111  func (req getRequestSyllab) LenOfSyllabHeap() (ln uint32) { return }