Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

大佬能提供一个获取afterImages中value的示例吗? #2

Open
Morriaty-The-Murderer opened this issue Jun 15, 2020 · 4 comments

Comments

@Morriaty-The-Murderer
Copy link

go gen avro 生成的 Union struct 好恶心啊,我目前只能使用switch case的方式获取

@fly512
Copy link

fly512 commented Jun 20, 2020

你运行的起来? @Morriaty-The-Murderer

@gua-pian
Copy link
Owner

go gen avro 生成的 Union struct 好恶心啊,我目前只能使用switch case的方式获取

可以把你的代码贴出来,看看?

@Morriaty-The-Murderer
Copy link
Author

@gua-pian 大致如下

fields := record.Fields.ArrayField
afterImages := record.AfterImages.ArrayUnionNullIntegerCharacterDecimalFloatTimestampDateTimeTimestampWithTimeZoneBinaryGeometryTextGeometryBinaryObjectTextObjectEmptyObject

esDoc := make(map[string]interface{})
var docID string
for idx, esField := range fields {
	r := afterImages[idx]
	if r == nil {
		continue
	}
	switch int(r.UnionType) {
	case 1:
		esDoc[esField] = r.Integer.Value
	case 2:
		esDoc[esField] = string(r.Character.Value)
	case 3:
		esDoc[esField] = r.Decimal.Value
	case 4:
		esDoc[esField] = r.Float.Value
	case 5:
		esDoc[esField] = r.Timestamp.Timestamp
	default:
		log.Infof("field <%s> after image <%+v> is ignored", fieldName, r)
	}
	if fieldName == "id" {
		docID = afterImages[idx].Integer.Value
	}
			
}

@gua-pian
Copy link
Owner

@Morriaty-The-Murderer 这个看上去没法很好的实现。或者可以参考 go-martini,做一个 map[reflect.Type] = reflect.Value 这样的 map 结构,辅助查询。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants