summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/magiconair/properties/example_test.go
blob: 6f21dfbd8e36a5b9a839bee8150963a0a2eee199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2017 Frank Schroeder. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package properties

import (
	"fmt"
	"log"
)

func ExampleLoad_iso88591() {
	buf := []byte("key = ISO-8859-1 value with unicode literal \\u2318 and umlaut \xE4") // 0xE4 == ä
	p, _ := Load(buf, ISO_8859_1)
	v, ok := p.Get("key")
	fmt.Println(ok)
	fmt.Println(v)
	// Output:
	// true
	// ISO-8859-1 value with unicode literal ⌘ and umlaut ä
}

func ExampleLoad_utf8() {
	p, _ := Load([]byte("key = UTF-8 value with unicode character ⌘ and umlaut ä"), UTF8)
	v, ok := p.Get("key")
	fmt.Println(ok)
	fmt.Println(v)
	// Output:
	// true
	// UTF-8 value with unicode character ⌘ and umlaut ä
}

func ExampleProperties_GetBool() {
	var input = `
	key=1
	key2=On
	key3=YES
	key4=true`
	p, _ := Load([]byte(input), ISO_8859_1)
	fmt.Println(p.GetBool("key", false))
	fmt.Println(p.GetBool("key2", false))
	fmt.Println(p.GetBool("key3", false))
	fmt.Println(p.GetBool("key4", false))
	fmt.Println(p.GetBool("keyX", false))
	// Output:
	// true
	// true
	// true
	// true
	// false
}

func ExampleProperties_GetString() {
	p, _ := Load([]byte("key=value"), ISO_8859_1)
	v := p.GetString("another key", "default value")
	fmt.Println(v)
	// Output:
	// default value
}

func Example() {
	// Decode some key/value pairs with expressions
	p, err := Load([]byte("key=value\nkey2=${key}"), ISO_8859_1)
	if err != nil {
		log.Fatal(err)
	}

	// Get a valid key
	if v, ok := p.Get("key"); ok {
		fmt.Println(v)
	}

	// Get an invalid key
	if _, ok := p.Get("does not exist"); !ok {
		fmt.Println("invalid key")
	}

	// Get a key with a default value
	v := p.GetString("does not exist", "some value")
	fmt.Println(v)

	// Dump the expanded key/value pairs of the Properties
	fmt.Println("Expanded key/value pairs")
	fmt.Println(p)

	// Output:
	// value
	// invalid key
	// some value
	// Expanded key/value pairs
	// key = value
	// key2 = value
}