123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- 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
- }
|