LioRoger / subscribe_example

subscribe from dts data store
63 stars 76 forks source link

golang : Incompatible types by name: &{com.alibaba.dts.formats.avro.Integer 0xc000148bd0} &{com.alibaba.dts.formats.avro.Decimal 0xc000149490} #21

Open fly512 opened 4 years ago

fly512 commented 4 years ago

如题,启动就报错,貌似 record.avsc 有问题

: Incompatible types by name: &{com.alibaba.dts.formats.avro.Integer 0xc000148bd0} &{com.alibaba.dts.formats.avro.Decimal 0xc000149490}

LioRoger commented 4 years ago

go我也不太熟悉,我在研究一下,这几天推一个可以work的版本

fly512 commented 4 years ago

看了下代码,即使解出来,如何遍历 AfterImages?

fly512 commented 4 years ago

把 avro 库 method.go compileRef 方法里的第一个 return fmt.Errorf 去掉 也能运行成功

qq1060656096 commented 4 years ago

看了下代码,即使解出来,如何遍历 AfterImages?

用了一个最笨的办法

  1. 按照联合类型来解析,获取值
  2. 然后转成map[string]interface
  3. 获取值的时候偷懒了 效果就是最后一张图,一直写php,dts没有php版本,这次入坑go。
    
    func (o *ALiYunDtsRecord) GetAfterImagesFields() (v []ALiYunDtsField, err error) {
    afterImagesArr := o.DtsAvroRecord.AfterImages.ArrayUnionNullIntegerCharacterDecimalFloatTimestampDateTimeTimestampWithTimeZoneBinaryGeometryTextGeometryBinaryObjectTextObjectEmptyObject
    return o.GetImagesFieldsRaw(afterImagesArr)
    }

func (o ALiYunDtsRecord) GetImagesFieldsRaw(dtsImagesArray []dtsavro.UnionNullIntegerCharacterDecimalFloatTimestampDateTimeTimestampWithTimeZoneBinaryGeometryTextGeometryBinaryObjectTextObjectEmptyObject) (v []ALiYunDtsField, err error) { l := len(o.DtsAvroRecord.Fields.ArrayField) v = make([]ALiYunDtsField, 0, l) for k, field := range o.DtsAvroRecord.Fields.ArrayField { o := &ALiYunDtsUnionObj{ DtsUnionNullObj: dtsImagesArray[k], } fieldValue, rowErr := o.GetValue() if rowErr != nil { err = rowErr } rv := ALiYunDtsField { Name: field.Name, Value: fieldValue, Err: err, } v = append(v, rv) } return }

func (o *ALiYunDtsRecord) ImagesFieldsToMap(s []ALiYunDtsField) (v map[string]interface{}, hasError bool, err error) { v = make(map[string]interface{}) for _, field := range s { v[field.Name] = field.Value if !hasError && field.Err != nil { hasError = true } } return }

```go
func GetStringValue(v interface{}) (string, bool) {
    switch rv := v.(type)  {
    case *string:
        return *rv, true
    case string:
        return rv, true
    case int, int8, int16, int32, int64:
        return fmt.Sprintf("%d", rv), true
    case uint, uint8, uint16, uint32, uint64:
        return fmt.Sprintf("%d", rv), true
    case float32, float64:
        return fmt.Sprintf("%f", rv), true
    case complex64, complex128:
        return fmt.Sprintf("%f", rv), true
    case bool:
        if rv {
            return "1", true
        }
        return "0", true
    }
    return "", false
}

image