package client import ( "errors" "git.sxidc.com/service-supports/dps-sdk/pb/v1/request" "git.sxidc.com/service-supports/dps-sdk/pb/v1/response" "reflect" "strings" "time" ) type TableRow struct { row map[string]any } func NewTableRow() *TableRow { return &TableRow{row: make(map[string]any)} } func (tableRow *TableRow) AddColumnValueTime(columnName string, value time.Time) *TableRow { tableRow.row[columnName] = value return tableRow } func (tableRow *TableRow) AddColumnValueBool(columnName string, value bool) *TableRow { tableRow.row[columnName] = value return tableRow } func (tableRow *TableRow) AddColumnValueString(columnName string, value string) *TableRow { tableRow.row[columnName] = value return tableRow } func (tableRow *TableRow) AddColumnValueInt(columnName string, value int) *TableRow { tableRow.row[columnName] = uint64(value) return tableRow } func (tableRow *TableRow) AddColumnValueInt8(columnName string, value int8) *TableRow { tableRow.row[columnName] = uint64(value) return tableRow } func (tableRow *TableRow) AddColumnValueInt16(columnName string, value int16) *TableRow { tableRow.row[columnName] = uint64(value) return tableRow } func (tableRow *TableRow) AddColumnValueInt32(columnName string, value int32) *TableRow { tableRow.row[columnName] = uint64(value) return tableRow } func (tableRow *TableRow) AddColumnValueInt64(columnName string, value int64) *TableRow { tableRow.row[columnName] = uint64(value) return tableRow } func (tableRow *TableRow) AddColumnValueUint(columnName string, value uint) *TableRow { tableRow.row[columnName] = uint64(value) return tableRow } func (tableRow *TableRow) AddColumnValueUint8(columnName string, value uint8) *TableRow { tableRow.row[columnName] = uint64(value) return tableRow } func (tableRow *TableRow) AddColumnValueUint16(columnName string, value uint16) *TableRow { tableRow.row[columnName] = uint64(value) return tableRow } func (tableRow *TableRow) AddColumnValueUint32(columnName string, value uint32) *TableRow { tableRow.row[columnName] = uint64(value) return tableRow } func (tableRow *TableRow) AddColumnValueUint64(columnName string, value uint64) *TableRow { tableRow.row[columnName] = value return tableRow } func (tableRow *TableRow) AddColumnValueFloat32(columnName string, value float32) *TableRow { tableRow.row[columnName] = float64(value) return tableRow } func (tableRow *TableRow) AddColumnValueFloat64(columnName string, value float64) *TableRow { tableRow.row[columnName] = value return tableRow } func (tableRow *TableRow) ToMap() map[string]any { return tableRow.row } func (tableRow *TableRow) SnapshotColumnValue(prefix string, version string, columnName string) any { value, ok := tableRow.row[prefix+"_snaps_"+version+"."+columnName] if !ok { return nil } return value } func (tableRow *TableRow) ColumnValueTime(columnName string) time.Time { value, ok := tableRow.row[columnName].(time.Time) if !ok { return time.Time{} } return value } func (tableRow *TableRow) ColumnValueBool(columnName string) bool { value, ok := tableRow.row[columnName].(bool) if !ok { return false } return value } func (tableRow *TableRow) ColumnValueString(columnName string) string { value, ok := tableRow.row[columnName].(string) if !ok { return "" } return value } func (tableRow *TableRow) ColumnValueInt(columnName string) int { value, ok := tableRow.row[columnName].(uint64) if !ok { return 0 } return int(value) } func (tableRow *TableRow) ColumnValueInt8(columnName string) int8 { value, ok := tableRow.row[columnName].(uint64) if !ok { return 0 } return int8(value) } func (tableRow *TableRow) ColumnValueInt16(columnName string) int16 { value, ok := tableRow.row[columnName].(uint64) if !ok { return 0 } return int16(value) } func (tableRow *TableRow) ColumnValueInt32(columnName string) int32 { value, ok := tableRow.row[columnName].(uint64) if !ok { return 0 } return int32(value) } func (tableRow *TableRow) ColumnValueInt64(columnName string) int64 { value, ok := tableRow.row[columnName].(uint64) if !ok { return 0 } return int64(value) } func (tableRow *TableRow) ColumnValueUint(columnName string) uint { value, ok := tableRow.row[columnName].(uint64) if !ok { return 0 } return uint(value) } func (tableRow *TableRow) ColumnValueUint8(columnName string) uint8 { value, ok := tableRow.row[columnName].(uint64) if !ok { return 0 } return uint8(value) } func (tableRow *TableRow) ColumnValueUint16(columnName string) uint16 { value, ok := tableRow.row[columnName].(uint64) if !ok { return 0 } return uint16(value) } func (tableRow *TableRow) ColumnValueUint32(columnName string) uint32 { value, ok := tableRow.row[columnName].(uint64) if !ok { return 0 } return uint32(value) } func (tableRow *TableRow) ColumnValueUint64(columnName string) uint64 { value, ok := tableRow.row[columnName].(uint64) if !ok { return 0 } return value } func (tableRow *TableRow) ColumnValueFloat32(columnName string) float32 { value, ok := tableRow.row[columnName].(float64) if !ok { return 0 } return float32(value) } func (tableRow *TableRow) ColumnValueFloat64(columnName string) float64 { value, ok := tableRow.row[columnName].(float64) if !ok { return 0 } return value } func (tableRow *TableRow) ToDPSTableRow() (*request.TableRow, error) { if tableRow.row == nil || len(tableRow.row) == 0 { return &request.TableRow{Columns: make([]*request.Column, 0)}, nil } columns := make([]*request.Column, 0) for columnName, value := range tableRow.row { valueType := reflect.TypeOf(value) typedValue := new(request.ColumnValue) typedValue.Type = valueType.Name() switch valueType.Name() { case "Time": timeObj := value.(time.Time) typedValue.TypedValue = &request.ColumnValue_Uint64Value{Uint64Value: uint64(timeObj.UnixMicro())} case "string": typedValue.TypedValue = &request.ColumnValue_StringValue{StringValue: value.(string)} case "bool": typedValue.TypedValue = &request.ColumnValue_BoolValue{BoolValue: value.(bool)} case "uint64": typedValue.TypedValue = &request.ColumnValue_Uint64Value{Uint64Value: value.(uint64)} case "float64": typedValue.TypedValue = &request.ColumnValue_Float64Value{Float64Value: value.(float64)} default: return nil, errors.New("不支持的数据类型" + valueType.Name()) } columns = append(columns, &request.Column{ Name: columnName, Value: typedValue, }) } return &request.TableRow{Columns: columns}, nil } func (tableRow *TableRow) FromDSPInfoData(infoData *response.InfoData) error { for _, column := range infoData.Columns { typeStr := column.Value.Type if typeStr == "Time" { timeObj := time.UnixMicro(int64(column.Value.GetUint64Value())) tableRow.row[column.Name] = timeObj } else if typeStr == "string" { tableRow.row[column.Name] = column.Value.GetStringValue() } else if typeStr == "bool" { tableRow.row[column.Name] = column.Value.GetBoolValue() } else if strings.Contains(typeStr, "int") { tableRow.row[column.Name] = column.Value.GetUint64Value() } else if strings.Contains(typeStr, "float") { tableRow.row[column.Name] = column.Value.GetFloat64Value() } else { return errors.New("不支持的数据类型" + column.Value.Type) } } return nil } func FromDSPInfosData(infosData []*response.InfoData) ([]TableRow, error) { tableRows := make([]TableRow, 0) for _, infoData := range infosData { tableRow := NewTableRow() err := tableRow.FromDSPInfoData(infoData) if err != nil { return nil, err } tableRows = append(tableRows, *tableRow) } return tableRows, nil }