Browse Source

添加文档

yjp 2 tháng trước cách đây
mục cha
commit
bbc2c40576
1 tập tin đã thay đổi với 29 bổ sung0 xóa
  1. 29 0
      framework/core/data_protocol/cloud_event.go

+ 29 - 0
framework/core/data_protocol/cloud_event.go

@@ -11,6 +11,15 @@ const (
 	DataContentTypeJson = "application/json"
 )
 
+// NewCloudEvent 创建CloudEvent
+// 参数:
+// - eventID: 事件ID
+// - eventType: 事件类型
+// - source: 事件源,如foo.com
+// - dataContentType: 事件数据类型,可以是任何mime类型,如application/json,application/text
+// - data: 要发送的数据
+// 返回值:
+// - CloudEvent
 func NewCloudEvent(eventID string, eventType string, source string, dataContentType string, data []byte) *CloudEvent {
 	return &CloudEvent{
 		SpecVersion:     "v1.0",
@@ -23,6 +32,15 @@ func NewCloudEvent(eventID string, eventType string, source string, dataContentT
 	}
 }
 
+// NewCloudEventJson 创建数据是Json类型的CloudEvent
+// 参数:
+// - eventID: 事件ID
+// - eventType: 事件类型
+// - source: 事件源,如foo.com
+// - obj: 需要转为json的对象
+// 返回值:
+// - CloudEvent
+// - 错误
 func NewCloudEventJson(eventID string, eventType string, source string, obj any) (*CloudEvent, error) {
 	dataJsonBytes, err := json.Marshal(obj)
 	if err != nil {
@@ -40,6 +58,12 @@ func NewCloudEventJson(eventID string, eventType string, source string, obj any)
 	}, nil
 }
 
+// UnmarshalJsonCloudEvent Unmarshal格式为Json的CloudEvent
+// 参数:
+// - cloudEventJsonBytes: CloudEvent的Json字节
+// 返回值:
+// - CloudEvent
+// - 错误
 func UnmarshalJsonCloudEvent(cloudEventJsonBytes []byte) (*CloudEvent, error) {
 	event := new(CloudEvent)
 	err := json.Unmarshal(cloudEventJsonBytes, event)
@@ -60,6 +84,11 @@ type CloudEvent struct {
 	Data            []byte `json:"data"`
 }
 
+// MarshalJson Marshal格式为Json的CloudEvent
+// 参数: 无
+// 返回值:
+// - CloudEvent的Json字节
+// - 错误
 func (event *CloudEvent) MarshalJson() ([]byte, error) {
 	if strutils.IsStringEmpty(event.SpecVersion) {
 		return nil, errors.New("没有传递事件规范版本")