Created
November 1, 2016 12:00
-
-
Save vinzenz/1bd1ed2ec9b76ed6bbdaccb232a69c61 to your computer and use it in GitHub Desktop.
Test code to test go-yaml make use of shared addresses when pointers are used to allow recursive parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"log" | |
"github.com/go-yaml/yaml" | |
) | |
type NoopRes struct { | |
Path string `yaml:"path"` | |
Content *string `yaml:"content"` // XXX *string | |
} | |
type Recursive struct { | |
Self *Recursive `yaml:"self"` | |
This *Recursive `yaml:"this"` | |
Name string `yaml:"name"` | |
} | |
type GraphConfig struct { | |
Graph string `yaml:"graph"` | |
Resources struct { | |
Noop []*NoopRes `yaml:"noop"` | |
Recursive Recursive `yaml:"other"` | |
} `yaml:"resources"` | |
Comment string `yaml:"comment"` | |
} | |
func (c *GraphConfig) Parse(data []byte) error { | |
if err := yaml.Unmarshal(data, c); err != nil { | |
return err | |
} | |
if c.Graph == "" { | |
return errors.New("Graph config: invalid `graph`") | |
} | |
return nil | |
} | |
func main() { | |
log.Printf("Hello!") | |
str := | |
` | |
--- | |
graph: mygraph | |
comment: my comment | |
resources: | |
noop: | |
- name: noop1 | |
path: foo | |
content: &ptr "hello" | |
- name: noop2 | |
path: bar | |
content: *ptr | |
- name: noop3 | |
path: baz | |
content: *ptr | |
unused: | |
- foo: bar | |
other: &recursive | |
self: *recursive | |
this: *recursive | |
name: Recursive item | |
` | |
data := []byte(str) | |
var config GraphConfig | |
if err := config.Parse(data); err != nil { | |
log.Printf("Config: Error: ParseConfigFromFile: Parse: %v", err) | |
return | |
} | |
log.Printf("Success!") | |
log.Printf("%+v", &config) | |
log.Printf("%+v", config.Resources.Noop[0].Content) | |
if x := config.Resources.Noop[0].Content; x != nil { | |
log.Printf("inside: %+v", *config.Resources.Noop[0].Content) | |
} | |
log.Printf("================") | |
log.Printf("%+v", config.Resources.Noop[1].Content) | |
if x := config.Resources.Noop[1].Content; x != nil { | |
log.Printf("inside: %+v", *x) | |
} else { | |
log.Printf("nothing inside!") | |
} | |
if config.Resources.Noop[0].Content != config.Resources.Noop[1].Content { | |
log.Printf("different pointers! :( %v %v", config.Resources.Noop[0].Content, config.Resources.Noop[1].Content) | |
} else { | |
log.Printf("same pointers! :)") | |
} | |
log.Printf("================") | |
log.Printf("%+v", config.Resources.Noop[2].Content) | |
if x := config.Resources.Noop[2].Content; x != nil { | |
log.Printf("inside: %+v", *x) | |
} else { | |
log.Printf("nothing inside!") | |
} | |
if config.Resources.Noop[1].Content != config.Resources.Noop[2].Content { | |
log.Printf("different pointers! :( %v %v", config.Resources.Noop[1].Content, config.Resources.Noop[2].Content) | |
} else { | |
log.Printf("same pointers! :)") | |
} | |
log.Printf("Recursive.This.Self.This.Name: %s", config.Resources.Recursive.This.Self.This.Name) | |
config.Resources.Recursive.This.Self.This.Name = "Modified Name" | |
log.Printf("Recursive.This.Self.This.Name: %s", config.Resources.Recursive.This.Self.This.Self.This.Name) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment