英文:
Datastore invalid value type for a Property with name
问题
我正在尝试使用GoLang中的PropertyList将int64数组保存到Datastore。
我知道你可以在Datastore中存储数组,尤其是原始类型的数组。所以我不确定在尝试通过PropertyList插入时出了什么问题。我的其他属性都能正确保存(它们只是单个的原始类型)。这个数组导致了这个问题。
英文:
I am trying to save an int64 array to Datastore using a PropertyList in GoLang.
Here is the Property in the PropertyList:
I know you can store arrays in Datastore, especially of primitives. So I'm not sure what I'm doing wrong, when trying to insert it via PropertyList. All my other properties get saved properly (they are just single primitives). The array is causing this issue.
答案1
得分: 5
解决了!感谢@mkopriva的帮助。
如果你想保存任何支持的Datastore数据类型的数组,你必须将该数组的每个元素追加到一个新的interface{}
数组中。
Value字段文档
我编写了这个反射函数来处理任何切片类型:
src := []int64{1, 2, 3, 4, 5}
value := reflect.ValueOf(src)
kind := value.Kind()
switch kind {
case reflect.Slice:
interfaceArr := make([]interface{},0)
for i := 0; i < value.Len(); i++ {
interfaceArr = append(interfaceArr, value.Index(i).Interface())
}
return interfaceArr
}
英文:
Solved! Thanks to @mkopriva for the help.
If you want to save an array of any supported Datastore data type, you must append each element of that array into a new interface{}
array.
Value Field Documentation
I wrote this reflect function to work with any slice type:
src := []int64{1, 2, 3, 4, 5}
value := reflect.ValueOf(src)
kind := value.Kind()
switch kind {
case reflect.Slice:
interfaceArr := make([]interface{},0)
for i := 0; i < value.Len(); i++ {
interfaceArr = append(interfaceArr, value.Index(i).Interface())
}
return interfaceArr
}
Here is the correct way an Array Property should look in a Property List:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论