github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/core/codec.go (about)

     1  package core
     2  
     3  import (
     4  	"github.com/15mga/kiwi"
     5  	"github.com/15mga/kiwi/util"
     6  )
     7  
     8  func InitCodec() {
     9  	kiwi.SetCodec(&codec{
    10  		fac:          make(map[kiwi.TSvcCode]util.ToMsg),
    11  		msgToSvcCode: make(map[string]kiwi.TSvcCode),
    12  		reqToRes:     make(map[kiwi.TSvcCode]kiwi.TCode),
    13  	})
    14  }
    15  
    16  type codec struct {
    17  	fac          map[kiwi.TSvcCode]util.ToMsg
    18  	msgToSvcCode map[string]kiwi.TSvcCode
    19  	reqToRes     map[kiwi.TSvcCode]kiwi.TCode
    20  }
    21  
    22  func (c *codec) PbMarshal(obj util.IMsg) ([]byte, *util.Err) {
    23  	if obj == nil {
    24  		return nil, nil
    25  	}
    26  	bytes, e := util.PbMarshal(obj)
    27  	if e != nil {
    28  		return nil, util.WrapErr(util.EcMarshallErr, e)
    29  	}
    30  	return bytes, nil
    31  }
    32  
    33  func (c *codec) PbUnmarshal(data []byte, msg util.IMsg) *util.Err {
    34  	if len(data) == 0 {
    35  		return nil
    36  	}
    37  	return util.WrapErr(util.EcUnmarshallErr, util.PbUnmarshal(data, msg))
    38  }
    39  
    40  func (c *codec) PbUnmarshal2(svc kiwi.TSvc, code kiwi.TCode, data []byte) (util.IMsg, *util.Err) {
    41  	fn, ok := c.fac[kiwi.MergeSvcCode(svc, code)]
    42  	if !ok {
    43  		return nil, util.NewErr(util.EcNotExist, util.M{
    44  			"svc":  svc,
    45  			"code": code,
    46  		})
    47  	}
    48  	msg := fn()
    49  	if len(data) == 0 {
    50  		return msg, nil
    51  	}
    52  	e := util.PbUnmarshal(data, msg)
    53  	if e != nil {
    54  		return nil, util.WrapErr(util.EcUnmarshallErr, e)
    55  	}
    56  	return msg, nil
    57  }
    58  
    59  func (c *codec) JsonMarshal(msg util.IMsg) ([]byte, *util.Err) {
    60  	if msg == nil {
    61  		return nil, nil
    62  	}
    63  	bytes, e := util.JsonMarshal(msg)
    64  	if e != nil {
    65  		return nil, util.WrapErr(util.EcMarshallErr, e)
    66  	}
    67  	return bytes, nil
    68  }
    69  
    70  func (c *codec) JsonUnmarshal(data []byte, msg util.IMsg) *util.Err {
    71  	if len(data) == 0 {
    72  		return nil
    73  	}
    74  	return util.WrapErr(util.EcUnmarshallErr, util.JsonUnmarshal(data, msg))
    75  }
    76  
    77  func (c *codec) JsonUnmarshal2(svc kiwi.TSvc, code kiwi.TCode, data []byte) (util.IMsg, *util.Err) {
    78  	fn, ok := c.fac[kiwi.MergeSvcCode(svc, code)]
    79  	if !ok {
    80  		return nil, util.NewErr(util.EcNotExist, util.M{
    81  			"service": svc,
    82  			"code":    code,
    83  		})
    84  	}
    85  	msg := fn()
    86  	if len(data) == 0 {
    87  		return msg, nil
    88  	}
    89  	e := util.JsonUnmarshal(data, msg)
    90  	if e != nil {
    91  		e.AddParams(util.M{
    92  			"service": svc,
    93  			"code":    code,
    94  			"data":    string(data),
    95  		})
    96  		return nil, util.WrapErr(util.EcUnmarshallErr, e)
    97  	}
    98  	return msg, nil
    99  }
   100  
   101  func (c *codec) Spawn(svc kiwi.TSvc, code kiwi.TCode) (util.IMsg, *util.Err) {
   102  	fn, ok := c.fac[kiwi.MergeSvcCode(svc, code)]
   103  	if !ok {
   104  		return nil, util.NewErr(util.EcNotExist, util.M{
   105  			"service": svc,
   106  			"code":    code,
   107  		})
   108  	}
   109  	return fn(), nil
   110  }
   111  
   112  func (c *codec) SpawnRes(svc kiwi.TSvc, code kiwi.TCode) (util.IMsg, *util.Err) {
   113  	m, err := c.ReqToResCode(svc, code)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  	return c.Spawn(svc, m)
   118  }
   119  
   120  func (c *codec) BindFac(svc kiwi.TSvc, code kiwi.TCode, fac util.ToMsg) {
   121  	sm := kiwi.MergeSvcCode(svc, code)
   122  	c.fac[sm] = fac
   123  	msg := fac()
   124  	msgName := string(msg.ProtoReflect().Descriptor().Name())
   125  	c.msgToSvcCode[msgName] = sm
   126  }
   127  
   128  func (c *codec) BindReqToRes(svc kiwi.TSvc, req, res kiwi.TCode) {
   129  	c.reqToRes[kiwi.MergeSvcCode(svc, req)] = res
   130  }
   131  
   132  func (c *codec) ReqToResCode(svc kiwi.TSvc, req kiwi.TCode) (kiwi.TCode, *util.Err) {
   133  	res, ok := c.reqToRes[kiwi.MergeSvcCode(svc, req)]
   134  	if !ok {
   135  		return 0, util.NewErr(util.EcNotExist, util.M{
   136  			"svc": svc,
   137  			"req": req,
   138  		})
   139  	}
   140  	return res, nil
   141  }
   142  
   143  func (c *codec) MsgToSvcCode(msg util.IMsg) (kiwi.TSvc, kiwi.TCode) {
   144  	name := string(msg.ProtoReflect().Descriptor().Name())
   145  	sc := c.msgToSvcCode[name]
   146  	return kiwi.SplitSvcCode(sc)
   147  }