Configuration language for Go is any good?
by
https://speakerdeck.com/linyows
×
Copy
Open
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Slide 1
Slide 1 text
HCL/TOMLศར @linyows / Fukuoka.go
Slide 2
Slide 2 text
@linyows / Tomohisa Oda GMO pepabo, incٕज़ج൫νʔϜ SCRAPTURE
Slide 3
Slide 3 text
͓Βͤ དྷि༵ʹ 5SBWJT.FFUVQJOԬɹ͕͋Γ·͢ 5SBWJT$*ͷ)JSP"TBSJ͞Μ͕དྷʢདྷʣ͞ΕΔͷͰɺ͓Λ ͬͨΓ࣭ͨ͠Γ͢ΔձͰ͢ɻ-5ืूதͰ͢ʂʂʂ IUUQLJCBOEPPSLFFQFSKQFWFOUT
Slide 4
Slide 4 text
ઃఆϑΝΠϧͲ͏ͯ͠Δʁ JSON / YAML / GO…
Slide 5
Slide 5 text
JSON { "foo": { "bar": "hello", "baz": { "key": 7, "hoge": true, "values": [0, 1, 2] } } } • なんか冗長すぎる • コメントが書けない • Structにtag書くの面倒 • 記述に迷う事はない
Slide 6
Slide 6 text
YAML foo: bar: hello baz: key: 7 hoge: off on: 'true' # comment~ values: [0, 1, 2] fuga: !str 123 "999": ~ • シンプルに書ける(コンパクト) • 色々な表現が出来る Anchor/Alias 型宣言 • 逆に敷居が高いのかも… キーにハイフンとか • でもまあ、yamlで良い気がするけど
Slide 7
Slide 7 text
͡Ό͋ GoͰ… func MyConfig() *Config { return &Config{ Bar: "hello", Baz: BazConfig{ Key: 7, Hoge: true, Values: []int{1, 2, 3}, }, } } • ださい…し、Goの知識がない人はつらい
Slide 8
Slide 8 text
HCL !?
Slide 9
Slide 9 text
HCLͱ HashiCorp Configuration Languageͷུ Vagrant, Serf…ͱ͔ͷHashCorp JSONޓ… JSONYAMLΛผͳݴޠʹஔ͖͑ΔͷͰͳ͍ผͷϨΠϠʔ JSONΛϑϨϯυϦʔʹͨ͠ͷ
Slide 10
Slide 10 text
Grammar hclData := ` a = "Easy!" b { c = 2 // comment~ d = [3, 4] e = true } ` variable "foo" { default = "bar" description = "bar" } variable "amis" { default = { east = "foo" } }
Slide 11
Slide 11 text
Decode type Config struct { A string B struct { C int D []int E bool } } hclObject, _ := hcl.Parse(hclData) fmt.Println(hclObject) // &{ 7 [0x2081f0240 0x2081f0390 0x2081f0480 0x2081f0600 0x2081f0690] } fmt.Println(hclObject.Get("b", false).Get("e", false).Value) // true var c Config hcl.DecodeObject(&c, hclObject) fmt.Println(c) // {Easy! {2 [3 4] true} false}
Slide 12
Slide 12 text
TOML !?
Slide 13
Slide 13 text
TOMLͱ Tom's Obvious, Minimal Languageͷུ Github Cofounderͷ Tom Preston-Werner (@mojombo) ϑϨϯυϦʔͳಡΈॻ͖ ύʔαΛॻ͘ͷ͕؆୯Β͍͠
Slide 14
Slide 14 text
Grammar hclData := ` A = "Easy!" ! [B] C = 2 D = [3, 4] E = true ` title = "TOML Example" ! [owner] name = "Tom Preston-Werner" organization = "GitHub" bio = "GitHub Cofounder & CEO\nLikes tater tots and beer." dob = 1979-05-27T07:32:00Z # First class dates? Why not? ! [database] server = "192.168.1.1" ports = [ 8001, 8001, 8002 ] connection_max = 5000 enabled = true [servers] ! # comment [servers.alpha] ip = "10.0.0.1" dc = "eqdc10" ! [servers.beta] ip = "10.0.0.2" dc = "eqdc10"
Slide 15
Slide 15 text
Decode type Config struct { A string B struct { C int D []int E bool } } var c Config meta, _ := toml.Decode(tomlData, &config) fmt.Println(config) // {Easy! {2 [3 4] true}} fmt.Println(meta) // {map[A:Easy! B:map[D:[3 4] E:true C:2]] map[A:String B:Hash B.C:Integer B.D:Array B.E:Bool] [[A] [B] [B C] [B D] [B E]] map[B.C:true B.D:true B.E:true A:true B:true] []}
Slide 16
Slide 16 text
͋Γ͕ͱ͏͍͟͝·ͨ͠