go - 为 TOML 文件和 golang 解析表中的键值对

标签 go toml

我有一个 TOML 文件的以下结构:

[database]
host = "localhost"
port = 8086
https = true
username = "root"
password = "root"
db = "test"

[cloud]
deviceType = "2be386e9bbae"
deviceId = "119a705fa3b1"
password = "test"
token = "dqpx5vNLLTR34"
endpoint = "mqtts://mqtt1.endpoint.com"

[gps]
#measurement = "gps"
  [gps.msgpack]
  topic = "/evt/gps/msgpack"

  [gps.json]
  topic = "/evt/gps/json"

[imu]
#measurement = "imu"
  [imu.1]
    tag = "NODE1"
    topic = "/evt/imu1/msgpack"
  [imu.2]
    tag = "NODE2"
    topic = "/evt/imu2/msgpack"

我想在 gps 表和 imu 表中只设置一次 measurement 键,而不是在 msgpack 中重复设置和 json 以及 12

使用注释掉的键下面的代码有效

代码

package main

import (
  "fmt"
  "github.com/BurntSushi/toml"
)

type imu struct {
  Topic string
  Measurement string
  Tag string
}

type gps struct {
  // Measurement string
  Measurement string
  ETopic string `toml:"topic"`
}

type database struct {
  Host  string
  Port  int
  Https bool
  Username  string
  Password  string
  Dbname  string
}

type cloud struct {
  Devicetype  string
  DeviceId  string
  Password  string
  Token   string
  Endpoint  string
}

type tomlConfig struct {
  DB database `toml:"database"`
  Cloud cloud `toml:"cloud"`
  Gps map[string]gps `toml:"gps"`
  Imu map[string]imu  `toml:"imu"`
}


func main()  {

  var config tomlConfig

  if _, err := toml.DecodeFile("cloud.toml", &config); err != nil {
    fmt.Println(err)
    return
  }
  // fmt.Printf("%#v\n", config)
  for sensorName, sensor := range config.Imu {
    fmt.Printf("Topic: %s %s %s %s\n", sensorName, sensor.Topic, sensor.Tag, sensor.Measurement)
  }

  for types, gps := range config.Gps {
    fmt.Printf("%s\n", types)
    fmt.Printf("%s\n", gps.ETopic)
  }
}

然而,在取消注释键值对时,我得到以下信息:

 toml: type mismatch for main.gps: expected table but found string

(它应该仍然是一个有效的 TOML,因为我将它翻译成 JSON 并检查了结构)

我知道我没有在 struct 中提到我需要为其添加一个字符串。但是,我对结构现在应该是什么样子感到困惑。

最佳答案

你说:

I want to set measurement key in gps table and imutable only once and not redundantly within msgpack and json and for 1 and 2

你不这样做是因为 TOML 格式的创建者说:

Because we need a decent human-readable format that unambiguously maps to a hash table and the YAML spec is like 80 pages long and gives me rage. No, JSON doesn't count. You know why.

如果你需要对一个键有相同的值,例如,measurement,你必须在每个子表中指定你想要的

您正确的 TOML 文件:

[database]
host = "localhost"
port = 8086
https = true
username = "root"
password = "root"
db = "test"

[cloud]
deviceType = "2be386e9bbae"
deviceId = "119a705fa3b1"
password = "test"
token = "dqpx5vNLLTR34"
endpoint = "mqtts://mqtt1.endpoint.com"

[gps]
[gps.msgpack]
topic = "/evt/gps/msgpack"
measurement = "gps"

[gps.json]
topic = "/evt/gps/json"
measurement = "gps"

[imu]
[imu.1]
measurement = "imu"
tag = "NODE1"
topic = "/evt/imu1/msgpack"
[imu.2]
measurement = "imu"
tag = "NODE2"
topic = "/evt/imu2/msgpack"

关于go - 为 TOML 文件和 golang 解析表中的键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53915596/

相关文章:

go - 将时间字符串类型解析回时间类型错误

go - 将 go 文件放在模块路径上是一个好习惯吗?

go - net/http Serve 方法何时返回错误?

performance - Go http 服务器基准测试性能慢

permalinks - Hugo 中的部分层次结构

json - TOML 是否支持对象/表的嵌套数组?

string - slice 后释放字符串以进行垃圾回收的正确方法

julia - 如何根据julia版本指定julia项目使用不同版本的JuMP

python - 如何使用 Python 从 TOML 文件中读取 Google API 凭据?

r - 我如何在我的 blogdown 站点的主页上发布一些介绍性段落?