将Firestore中的”integer_value”转换为整数类型。

huangapple go评论92阅读模式
英文:

Casting firestore "integer_value" to integer

问题

使用Golang Firestore 1.8库,我正在尝试使用Google的Firestore在去年引入的新的count()函数。文档似乎还没有示例,至少我没有找到,但我已经拼凑出了一段基本可行的代码,几乎可以实现目标,只是没有生成一个整数。我对代码片段底部的"result[userCountAlias]"值感兴趣,我不太确定如何将其转换为整数。当然,我可以将其作为字符串,以冒号为分隔符进行拆分,然后解析它,但那看起来很丑陋。

非常感谢您的帮助!

func (s UserService) Count(labID string) (int64, error) {

	if s.DB == nil {
		return -1, customerrors.ErrDatabaseMissing
	}

	query := s.DB.
		Collection(CollectionUsers).
		Where("lab_id", "==", labID)


	userCountAlias := "userCount"

	ag := query.NewAggregationQuery()

    //result是一个firestore.AggregationResult,似乎只包含一个map[string]interface{}
	result, err := ag.WithCount(userCountAlias).Get(s.Ctx)

	if err != nil {
		return -1, err
	}

	v := result[userCountAlias]//我如何将其转换为整数?
	fmt.Printf("Type = %v", v) //打印出"Type = integer_value:379"

	return -1, nil
}
英文:

Using the Golang Firestore 1.8 library, I'm trying to use the new-ish count() functions Google's Firestore introduced last fall. The docs don't seem to have examples yet, not that I've found, but I've cobbled together a somewhat-workable bit of code that gets me almost all the way, just short of actually producing an integer. The "result[userCountAlias]" value towards the bottom of this snippet is what I'm interested in converting to an integer, and I'm not quite sure how. I could, of course, take it as a string, split on the colon, and then parse it, but that seems ugly.

Any hints would be much appreciated!

Many thanks.

func (s UserService) Count(labID string) (int64, error) {

	if s.DB == nil {
		return -1, customerrors.ErrDatabaseMissing
	}

	query := s.DB.
		Collection(CollectionUsers).
		Where("lab_id", "==", labID)


	userCountAlias := "userCount"

	ag := query.NewAggregationQuery()

    //result is a firestore.AggregationResult, which seems to consist of just a 
    //map[string]interface{}
	result, err := ag.WithCount(userCountAlias).Get(s.Ctx)

	if err != nil {
		return -1, err
	}

	v := result[userCountAlias]//How do I cast this to an integer?
	fmt.Printf("Type = %v", v) //Prints "Type = integer_value:379"

	return -1, nil
}

答案1

得分: 3

尝试使用fmt.Printf("Type = %T", v)来查找v的类型。

v很可能是类型为firestorepb.Value的变量。请注意,这在1.8版本中不可用。尝试将cloud.google.com/go/firestore升级到最新版本(目前是1.9)。

package main

import (
	"fmt"

	"cloud.google.com/go/firestore/apiv1/firestorepb"
)

func main() {
	var v interface{} = &firestorepb.Value{
		ValueType: &firestorepb.Value_IntegerValue{
			IntegerValue: 379,
		},
	}

	fmt.Printf("%T\n", v) // *firestorepb.Value
	fmt.Printf("%v\n", v) // integer_value:379

	if v, ok := v.(*firestorepb.Value); ok {
		fmt.Printf("%v\n", v.GetIntegerValue()) // 379
	}
}

官方存储库中的测试以相同的方式检索值。请参见TestIntegration_CountAggregationQuery

英文:

Try fmt.Printf("Type = %T", v) to find out the type of v.

v is most likely of type firestorepb.Value. Please note that this is not available in 1.8 yet. Try to upgrade cloud.google.com/go/firestore to its latest version (it's 1.9 as of now).

package main

import (
	"fmt"

	"cloud.google.com/go/firestore/apiv1/firestorepb"
)

func main() {
	var v interface{} = &firestorepb.Value{
		ValueType: &firestorepb.Value_IntegerValue{
			IntegerValue: 379,
		},
	}

	fmt.Printf("%T\n", v) // *firestorepb.Value
	fmt.Printf("%v\n", v) // integer_value:379

	if v, ok := v.(*firestorepb.Value); ok {
		fmt.Printf("%v\n", v.GetIntegerValue()) // 379
	}
}

The test in the official repository retrieves the value in the same way. See TestIntegration_CountAggregationQuery.

huangapple
  • 本文由 发表于 2023年4月11日 11:50:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定