gpt4 book ai didi

go - 类型在Golang Api种类结构创建上发生冲突

转载 作者:行者123 更新时间:2023-12-01 22:41:18 26 4
gpt4 key购买 nike

在client-go中创建types.go时遇到问题。

kind: Kafka
metadata:
name: my-cluster
namespace: sample-system
spec:
kafka:
version: 2.5.0
replicas: 3
listeners:
plain:
authentiation:
type: scram-sha-512
tls:
authentiation:
type: tls
按照以下种类 “Kafka” ...
我有如下的卡夫卡种类的架构

// Kafka is the Schema for the kafkas API
type Kafka struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec KafkaSpec `json:"spec,omitempty"`
Status KafkaStatus `json:"status,omitempty"`
}
而当我需要定义Spec.kafka类型时
// KafkaSpec defines the desired state of KafkaBundle
type KafkaSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
Kafka *Kafka `json:"kafka"`

// Foo is an example field of Kafka. Edit Kafka_types.go to remove/update
Foo string `json:"foo,omitempty"`
}

我遇到两个卡夫卡结构类型的问题。这是错误的。

最佳答案

您的struct Kafka有一个相关的struct KafkaSpec,而您的KafkaSpec则取决于struct Kafka。如果您研究结构,那么您正在创建循环依赖关系,这在这里可能是个问题。我建议遵循以下结构,
主要结构Kafka

// Kafka is the Schema for the kafkas API
type Kafka struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec KafkaSpec `json:"spec,omitempty"`
Status KafkaStatus `json:"status,omitempty"`
}
结构 Spec,它是 Kafka的子级。
// Spec defines the desired state of KafkaBundle
type Spec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
Kafka *KafkaSpec `json:"kafka"`

// Foo is an example field of Kafka. Edit Kafka_types.go to remove/update
Foo string `json:"foo,omitempty"`
}
结构 KafkaSpec是struct Spec的子代
type KafkaSpec struct {
Replicas int `json:"replicas"`
Listeners *Listners
}
同样,为 PlainAuthentication创建一个结构。
我希望这能解决您的问题。

关于go - 类型在Golang Api种类结构创建上发生冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63559102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com