I am trying to use resgate with a nestjs microservice server backend, and want to add a new codec to work with nestjs nats message.
The resgate message is packaged as nest message data. and resgate response is got from nestjs response message.
type NestGetRequest struct {
ID string `json:"id"`
Pattern interface{} `json:"pattern,omitempty"`
Data GetRequest `json:"data",omitempty"`
}
func DecodeGetResponse(payload []byte) (*GetResult, error) {
var nestR NestResponse
err := json.Unmarshal(payload, &nestR)
if err != nil {
return nil, reserr.InternalError(err)
}
if nestR.Response == nil {
return nil, errMissingResult
}
var r GetResponse
err = json.Unmarshal(nestR.Response, &r)
if err != nil {
return nil, reserr.InternalError(err)
}
if r.Error != nil {
return nil, r.Error
}
if r.Result == nil {
return nil, errMissingResult
}
// Assert we got either a model or a collection
res := r.Result
if res.Model != nil {
if res.Collection != nil {
return nil, errInvalidResponse
}
// Assert model only has proper values
for _, v := range res.Model {
if v.Type != ValueTypeResource && v.Type != ValueTypePrimitive {
return nil, errInvalidResponse
}
}
} else {
if res.Collection == nil {
return nil, errInvalidResponse
}
// Assert collection only has proper values
for _, v := range res.Collection {
if v.Type != ValueTypeResource && v.Type != ValueTypePrimitive {
return nil, errInvalidResponse
}
}
}
return r.Result, nil
}