gpt4 book ai didi

go - 无法使用带有 BurntSushi 库的 Go 读取 TOML 文件

转载 作者:数据小太阳 更新时间:2023-10-29 03:13:48 25 4
gpt4 key购买 nike

我正在使用 BurntSushi 库在我的 GO 应用程序中加载 TOML 配置文件。我已按照库中的说明编写结构和配置 toml 文件本身。我遇到了一些麻烦,而且我似乎找不到问题的根源。

详情如下:

结构:

package main


//ConfigurationParameters provides the struct to hold configuration parameters from config file
type ConfigurationParameters struct {
Title string
//serviceDiscovery captures configuration parameters needed for service discovery registration with Consul
ServiceDiscovery ConsulConf `toml:"ServiceDiscovery"`
//metadataReporting captures which metadata to be registered with service into consul for use during discovery
MetadataReporting MetaDataConf `toml:"MetadataReporting"`
//awsTagsToLabels captures the aws tags that should be added to reported metrics as Labels
AwsTagsToLabels LabelConf `toml:"AwsTagsToLabels"`
//collectors captures the list of collectors to use
Collectors CollectorConf `toml:"Collectors"`
//service captures agent related configurations
Service ServiceConf `toml:"Service"`
}

//ConsulConf captures configuration parameters needed for service discovery registration with Consul
type ConsulConf struct {
enabled bool
endpoint string
port int
datacenter string
serviceID string
}
//MetaDataConf captures which metadata to be registered with service into consul for use during discovery
type MetaDataConf struct {
enabled bool
awsregion string
}
//LabelConf captures the aws tags that should be added to reported metrics as Labels
type LabelConf struct {
enabled bool
refreshPeriod int
}

//CollectorConf captures the list of collectors to use
type CollectorConf struct {
goCollectionEnabled bool
exporterCollectionEnabled bool
wmiCollectionEnabled bool
agentCollectionEnabled bool
enabledCollectors string
metricMap []MetricMap
}

//MetricMap captures a mapping between one or more WMI metrics and the name it should be reported with
type MetricMap struct {
wmiMetricName []string
exportName string
dropMetric bool
computedMetric bool
computeLogic string
}

//ServiceConf captures agent related configurations
type ServiceConf struct {
listenIP string
listenPort int
metricPath string
collectionInterval int
serviceName string
}

和配置toml文件:

Title = "WMI Exporter Configuration"

[ServiceDiscovery]
enabled = true
endpoint = "my.consul.server"
port = 5500
datacenter = "ucm-west"
serviceID = "ucm.agent.wmiExporter"

[MetadataReporting]
enabled = true
awsregion = "us-west-2"

[AwsTagsToLabels]
enabled = true
refreshPeriod = 3600

[Collectors]
goCollectionEnabled = true
exporterCollectionEnabled = true
wmiCollectionEnabled = true
agentCollectionEnabled = false
enabledCollectors = "cpu,os"
[Collectors.MetricMap.0]
wmiMetricName = ["test"]
exportName = "export_test"

[Service]
listenPort = 9103
metricPath = "/metrics"
collectionInterval = 60
serviceName = "wmi_exporter"

读取配置文件的代码:

// InitializeFromConfig reads configuration parameters from configuration file and initializes this service
func InitializeFromConfig(configfile string) ConfigurationParameters {
conf := ConfigurationParameters{}

if configfile == "" {
return conf
}

_, err := toml.DecodeFile(configfile, &conf)
if err != nil {
log.Fatalf("Cannot parse configuration file at %s. Error=%s", configfile, err)
}
//at this point, conf is a fully loaded configuration now; now initialize everything from conf
return conf
}

我面临的问题是只有 Title 属性的值被映射到 GO 结构成员。所有其他配置保持未映射状态。查看 github 上关于 BurntSushi 和 (Go) How to use toml files? 的所有示例,我看不出我目前在代码中所做的有什么不同。

我还使用了 BurntSushi 包中的 tomlv 验证器来查看 TOML 文件中的类型,我相信它们看起来是正确的。

数据类型:

Title                                             String
ServiceDiscovery Hash
ServiceDiscovery.enabled Bool
ServiceDiscovery.endpoint String
ServiceDiscovery.port Integer
ServiceDiscovery.datacenter String
ServiceDiscovery.serviceID String
MetadataReporting Hash
MetadataReporting.enabled Bool
MetadataReporting.awsregion String
AwsTagsToLabels Hash
AwsTagsToLabels.enabled Bool
AwsTagsToLabels.refreshPeriod Integer
Collectors Hash
Collectors.goCollectionEnabled Bool
Collectors.exporterCollectionEnabled Bool
Collectors.wmiCollectionEnabled Bool
Collectors.agentCollectionEnabled Bool
Collectors.enabledCollectors String
Collectors.MetricMap.0 Hash
Collectors.MetricMap.0.wmiMetricName Array
Collectors.MetricMap.0.exportName String
Service Hash
Service.listenPort Integer
Service.metricPath String
Service.collectionInterval Integer
Service.serviceName String

我尝试调试 BurntSushi 包代码,但它不是很有帮助(Delve 调试器无法显示该包中的某些变量,并且似乎在该包中的行之间随机跳转)。

关于我可能做错了什么的任何帮助或指示?

谢谢。

最佳答案

您需要导出您希望 BurntSushi/toml 解码到的任何字段,包括子字段:

//ConfigurationParameters provides the struct to hold configuration parameters from config file
type ConfigurationParameters struct {
Title string
//serviceDiscovery captures configuration parameters needed for service discovery registration with Consul
ServiceDiscovery ConsulConf `toml:"ServiceDiscovery"`
//metadataReporting captures which metadata to be registered with service into consul for use during discovery
MetadataReporting MetaDataConf `toml:"MetadataReporting"`
//awsTagsToLabels captures the aws tags that should be added to reported metrics as Labels
AwsTagsToLabels LabelConf `toml:"AwsTagsToLabels"`
//collectors captures the list of collectors to use
Collectors CollectorConf `toml:"Collectors"`
//service captures agent related configurations
Service ServiceConf `toml:"Service"`
}

//ConsulConf captures configuration parameters needed for service discovery registration with Consul
type ConsulConf struct {
Enabled bool
Endpoint string
Port int
Datacenter string
ServiceID string
}

//MetaDataConf captures which metadata to be registered with service into consul for use during discovery
type MetaDataConf struct {
Enabled bool
Awsregion string
}

//LabelConf captures the aws tags that should be added to reported metrics as Labels
type LabelConf struct {
Enabled bool
RefreshPeriod int
}

//CollectorConf captures the list of collectors to use
type CollectorConf struct {
GoCollectionEnabled bool
ExporterCollectionEnabled bool
WmiCollectionEnabled bool
AgentCollectionEnabled bool
EnabledCollectors string
MetricMap []MetricMap
}

//MetricMap captures a mapping between one or more WMI metrics and the name it should be reported with
type MetricMap struct {
WmiMetricName []string
ExportName string
DropMetric bool
ComputedMetric bool
ComputeLogic string
}

//ServiceConf captures agent related configurations
type ServiceConf struct {
ListenIP string
ListenPort int
MetricPath string
CollectionInterval int
ServiceName string
}

此外,我不确定此 Collectors.MetricMap.0 语法应该表示什么,而是将您的 toml 值解码到 []MetricMap 字段中你想要做的是这样的:

[[Collectors.MetricMap]]
wmiMetricName = ["test"]
exportName = "export_test"

关于go - 无法使用带有 BurntSushi 库的 Go 读取 TOML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43218484/

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