将继承的结构体作为基本对象返回

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

Passing an inherited struct back as the base object

问题

我确定这是一个我还没有解决的Go语法问题。

我得到的错误信息是:

无法将*term(类型为elastic.AggregationBucketKeyItem)作为参数传递给extractBucket中的elastic.Aggregations类型

生成错误的代码行是:

"Value": extractBucket(parts[1:], *term),

相关的代码如下:

// 来自 https://github.com/olivere/elastic/blob/v3.0.22/search_aggs.go

type Aggregations map[string]*json.RawMessage

type AggregationBucketSignificantTerms struct {
    Aggregations

    DocCount int64                               //`json:"doc_count"`
    Buckets  []*AggregationBucketSignificantTerm //`json:"buckets"`
    Meta     map[string]interface{}              // `json:"meta,omitempty"`
}

// 我的代码

func extractBucket(parts []string, aggs elastic.Aggregations) interface{} {
    // 移除了一堆代码

    terms, found := aggs.Terms(part)
    for _, term := range terms.Buckets {
        if len(parts) == 0 {
            retval[(term.Key).(string)] = map[string]interface{}{
                "Count": term.DocCount,
            }
        } else {
            retval[(term.Key).(string)] = map[string]interface{}{
                "Count": term.DocCount,
                "Value": extractBucket(parts[1:], *term),
            }
        }
    }
}

以上是要翻译的内容。

英文:

I'm sure this is a syntax issue that I've yet to figure out with Go -

The error I'm getting --
> cannot use *term (type elastic.AggregationBucketKeyItem) as type elastic.Aggregations in argument to extractBucket

The line that's generating the error is

"Value": extractBucket(parts[1:], *term),

The relevant code, for context

// from https://github.com/olivere/elastic/blob/v3.0.22/search_aggs.go

type Aggregations map[string]*json.RawMessage

type AggregationBucketSignificantTerms struct {
    Aggregations

    DocCount int64                               //`json:"doc_count"`
    Buckets  []*AggregationBucketSignificantTerm //`json:"buckets"`
    Meta     map[string]interface{}              // `json:"meta,omitempty"`
}

// my code

func extractBucket(parts []string, aggs elastic.Aggregations) interface{} {
    // bunch of code removed

           terms, found := aggs.Terms(part)
           for _, term := range terms.Buckets {
            if len(parts) == 0 {
                retval[(term.Key).(string)] = map[string]interface{}{
                    "Count": term.DocCount,
                }
            } else {
                retval[(term.Key).(string)] = map[string]interface{}{
                    "Count": term.DocCount,
                    "Value": extractBucket(parts[1:], *term),
                }
            }
        }
}

答案1

得分: 2

错误的原因很明显:

>在提取桶时,无法将(*term, 变量名)(类型为elastic.AggregationBucketKeyItem,当前类型为变量)作为参数传递给extractBucket(类型为elastic.Aggregations,期望类型为elastic.Aggregations)。

无论您的*term值是什么,

生成代码为:for _, term := range terms.Buckets {

该函数的参数类型应为:

extractBucket(parts []string, aggs elastic.Aggregations)

需要传入elastic.Aggregations类型的参数。

英文:

Well the error is pretty self explanatory:

>cannot use (*term, variable name) (type elastic.AggregationBucketKeyItem <-- Variables current type) as (type elastic.Aggregations <-- Expected type) in argument to extractBucket

Whatever your *term value

Generated by: for _, term := range terms.Buckets {

is not the right type for the function

extractBucket(parts []string, aggs elastic.Aggregations)

Takes a type of elastic.Aggregations

答案2

得分: 2

这是一个常见的误解,认为嵌入一个类型会使你“继承”该类型。尽管AggregationBucketSignificantTerms 嵌入Aggregations,但对编译器来说它并不是一个Aggregations类型。它只是具有一个Aggregations类型的字段,并在其顶层提供该类型的方法。这感觉有点像继承,但可能与Java子类的用法不同。

要解决这个问题,你可以尝试使用"Value": extractBucket(parts[1:], *term.Aggregations),但我不确定这是否能解决你的问题。

英文:

It is a common misunderstanding that embedding a type makes you "inherit" that type. Even though AggregationBucketSignificantTerms embeds an Aggregations, it is not one to the compiler. It merely has a field of type Aggregations, and it provides methods from that type at it's top level. It feels a bit like inheritance, but is probably not what you are used to with things like Java subclasses.

To solve it you can try &quot;Value&quot;: extractBucket(parts[1:], *term.Aggregations),, but I am not clear if that will solve your problem or not.

huangapple
  • 本文由 发表于 2016年2月29日 08:00:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/35689930.html
匿名

发表评论

匿名网友

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

确定