func unpackResponseBody()

in response.go [151:227]


func unpackResponseBody(decoder *Decoder, resp interface{}) error {
	// body
	if decoder == nil {
		return perrors.Errorf("@decoder is nil")
	}
	rspType, err := decoder.Decode()
	if err != nil {
		return perrors.WithStack(err)
	}

	response := EnsureResponse(resp)

	switch rspType {
	case RESPONSE_WITH_EXCEPTION, RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS:
		expt, decErr := decoder.Decode()
		if decErr != nil {
			return perrors.WithStack(decErr)
		}
		if rspType == RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS {
			attachments, attErr := decoder.Decode()
			if attErr != nil {
				return perrors.WithStack(attErr)
			}
			if v, ok := attachments.(map[interface{}]interface{}); ok {
				atta := ToMapStringString(v)
				response.Attachments = atta
			} else {
				return perrors.Errorf("get wrong attachments: %+v", attachments)
			}
		}

		if e, ok := expt.(error); ok {
			response.Exception = e
		} else {
			response.Exception = perrors.Errorf("got exception: %+v", expt)
		}
		return nil

	case RESPONSE_VALUE, RESPONSE_VALUE_WITH_ATTACHMENTS:
		rsp, decErr := decoder.Decode()
		if decErr != nil {
			return perrors.WithStack(decErr)
		}
		if rspType == RESPONSE_VALUE_WITH_ATTACHMENTS {
			attachments, attErr := decoder.Decode()
			if attErr != nil {
				return perrors.WithStack(attErr)
			}
			if v, ok := attachments.(map[interface{}]interface{}); ok {
				response.Attachments = ToMapStringString(v)
			} else {
				return perrors.Errorf("get wrong attachments: %+v", attachments)
			}
		}

		response.RspObj = rsp

		return nil

	case RESPONSE_NULL_VALUE, RESPONSE_NULL_VALUE_WITH_ATTACHMENTS:
		if rspType == RESPONSE_NULL_VALUE_WITH_ATTACHMENTS {
			attachments, decErr := decoder.Decode()
			if decErr != nil {
				return perrors.WithStack(decErr)
			}
			if v, ok := attachments.(map[interface{}]interface{}); ok {
				atta := ToMapStringString(v)
				response.Attachments = atta
			} else {
				return perrors.Errorf("get wrong attachments: %+v", attachments)
			}
		}
		return nil
	}

	return nil
}