I have the following structure for a TOML file:
[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"
measurementgpsimumsgpackjson12
With the commented out keys The following code works
Code
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
", config)
for sensorName, sensor := range config.Imu {
fmt.Printf("Topic: %s %s %s %s
", sensorName, sensor.Topic, sensor.Tag, sensor.Measurement)
}
for types, gps := range config.Gps {
fmt.Printf("%s
", types)
fmt.Printf("%s
", gps.ETopic)
}
}
However on uncommenting out the key value pair I get the following:
toml: type mismatch for main.gps: expected table but found string
(It should still be a valid TOML, as I translated it to JSON and checked the structure)
struct