代码之家  ›  专栏  ›  技术社区  ›  Shan-Desai askovpen

分析表中toml文件和golang的键值对

  •  0
  • Shan-Desai askovpen  · 技术社区  · 6 年前

    对于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"
    

    我想设置 measurement 键入 gps 表及 imu 表仅一次,不在 msgpack json 为了 1 2

    使用注释掉的键,以下代码可以工作

    代码

    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 我需要为它添加一个字符串。然而,我对结构现在应该是什么样子感到困惑。

    1 回复  |  直到 6 年前
        1
  •  2
  •   zwirec    6 年前

    你说:

    我想在GPS表中设置测量键,仅可修改一次,并且在msgpack和json以及1和2中不冗余。

    你不能这样做,因为toml格式的创建者说:

    因为我们需要一种人类可读的格式,它明确地映射到哈希表,而yaml规范就像80页长,这让我很愤怒。不,JSON不算数。你知道为什么。

    例如,如果需要对一个键使用相同的值, 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"
    
    推荐文章