import_config.go 1.2 KB
Newer Older
X
xieyinglin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package dataimport

import (
	"encoding/json"
	"fmt"
	"path/filepath"
	"sync"

	"github.com/pelletier/go-toml"
)

var (
	cfg  Config
	once sync.Once
)

张金富 已提交
17
// Config include all scene import config
X
xieyinglin 已提交
18 19 20 21 22 23
type Config struct {
	UserCases map[string]CaseConfig
}

// CaseConfig include the sample data config and tdengine config
type CaseConfig struct {
张金富 已提交
24 25 26 27 28 29 30 31 32 33
	Format              string
	FilePath            string
	Separator           string
	StName              string
	SubTableName        string
	Timestamp           string
	TimestampType       string
	TimestampTypeFormat string
	Tags                []FieldInfo
	Fields              []FieldInfo
X
xieyinglin 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
}

// FieldInfo is field or tag info
type FieldInfo struct {
	Name string
	Type string
}

// LoadConfig will load the specified file config
func LoadConfig(filePath string) Config {
	once.Do(func() {
		filePath, err := filepath.Abs(filePath)
		if err != nil {
			panic(err)
		}
		fmt.Printf("parse toml file once. filePath: %s\n", filePath)
		tree, err := toml.LoadFile(filePath)
		if err != nil {
			panic(err)
		}

		bytes, err := json.Marshal(tree.ToMap())
		if err != nil {
			panic(err)
		}

		err = json.Unmarshal(bytes, &cfg.UserCases)
		if err != nil {
			panic(err)
		}
	})
	return cfg
}