コード例 #1
0
ファイル: example_test.go プロジェクト: smithjm/etcd-export
// ExampleNestedStruct creates a Etcd directory using a nested Go struct and then gets the directory as JSON.
func Example_nestedStruct() {
	verbose := flag.Bool("verbose", false, "Verbose")
	node := flag.String("node", "", "Etcd node")
	port := flag.String("port", "", "Etcd port")
	flag.Parse()

	// Define nested structure.
	g := Group{
		Name: "staff",
		Users: []User{
			User{
				Name:      "jdoe",
				Age:       25,
				Male:      true,
				FirstName: "John",
				LastName:  "Doe",
			},
			User{
				Name:      "lnemoy",
				Age:       62,
				Male:      true,
				FirstName: "Leonard",
				LastName:  "Nimoy",
			},
		},
	}

	// Connect to Etcd.
	conn := getEnv()
	if node == nil && port == nil {
		conn = []string{fmt.Sprintf("http://%v:%v", *node, *port)}
	}

	if *verbose {
		log.Printf("Connecting to: %s", conn)
	}
	client := etcd.NewClient(conn)

	// Create directory structure based on struct.
	err := etcdmap.Create(client, "/example", reflect.ValueOf(g))
	if err != nil {
		log.Fatal(err.Error())
	}

	// Get directory structure from Etcd.
	res, err := client.Get("/example", true, true)
	if err != nil {
		log.Fatal(err.Error())
	}

	j, err2 := etcdmap.JSON(res.Node)
	if err2 != nil {
		log.Fatal(err2.Error())
	}

	fmt.Println(string(j))

	// Output:
	//{"id":"staff","users":{"0":{"age":"25","first_name":"John","id":"jdoe","last_name":"Doe","male":"true"},"1":{"age":"62","first_name":"Leonard","id":"lnemoy","last_name":"Nimoy","male":"true"}}}
}
コード例 #2
0
ファイル: example_test.go プロジェクト: mickep76/etcdrest
// ExampleNestedStruct creates a Etcd directory using a nested Go struct and then gets the directory as JSON.
func Example_nestedStruct() {
	// Get env variables.
	env := getEnv()

	// Options.
	peers := flag.String("peers", env.Peers, "Comma separated list of etcd nodes, can be set with env. variable ETCD_PEERS")
	flag.Parse()

	// Define nested structure.
	g := Group{
		Name: "staff",
		Users: []User{
			User{
				Name:      "jdoe",
				Age:       25,
				Male:      true,
				FirstName: "John",
				LastName:  "Doe",
			},
			User{
				Name:      "lnemoy",
				Age:       62,
				Male:      true,
				FirstName: "Leonard",
				LastName:  "Nimoy",
			},
		},
	}

	// Connect to etcd.
	cfg := etcd.Config{
		Endpoints:               strings.Split(*peers, ","),
		Transport:               etcd.DefaultTransport,
		HeaderTimeoutPerRequest: time.Second,
	}

	client, err := etcd.New(cfg)
	if err != nil {
		log.Fatal(err)
	}

	kapi := etcd.NewKeysAPI(client)

	// Create directory structure based on struct.
	err2 := etcdmap.Create(kapi, "/example", reflect.ValueOf(g))
	if err2 != nil {
		log.Fatal(err2.Error())
	}

	// Get directory structure from Etcd.
	res, err3 := kapi.Get(context.TODO(), "/example", &etcd.GetOptions{Recursive: true})
	if err3 != nil {
		log.Fatal(err3.Error())
	}

	j, err4 := etcdmap.JSON(res.Node)
	if err4 != nil {
		log.Fatal(err4.Error())
	}

	fmt.Println(string(j))

	// Get directory structure from Etcd.
	/*
		res, err5 := kapi.Get(context.TODO(), "/example/users/0", &etcd.GetOptions{Recursive: true})
		if err5 != nil {
			log.Fatal(err5.Error())
		}

		s := User{}
		err6 := etcdmap.Struct(res.Node, reflect.ValueOf(&s))
		if err6 != nil {
			log.Fatal(err6.Error())
		}

		fmt.Println(s)
	*/

	// Output:
	//{"name":"staff","users":{"0":{"age":"25","first_name":"John","last_name":"Doe","male":"true","name":"jdoe"},"1":{"age":"62","first_name":"Leonard","last_name":"Nimoy","male":"true","name":"lnemoy"}}}
}