Repository navigation
Expand file tree
/
Copy pathdoc.go
More file actions
208 lines (156 loc) · 5.13 KB
/
Copy pathdoc.go
File metadata and controls
208 lines (156 loc) · 5.13 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (C) 2024 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
/*
Package toml provides a standalone TOML v1.0.0 parser for Go.
# Overview
This package implements a read-only TOML parser that supports all features
of the TOML v1.0.0 specification. It provides both flat dot-notation access
and hierarchical navigation for working with TOML documents.
# Basic Usage
Parse a TOML file and access values:
doc, err := toml.Parse("config.toml")
if err != nil {
log.Fatal(err)
}
// Flat access with dot notation
host, _ := doc.GetString("server.host")
port, _ := doc.GetInt("server.port")
// Hierarchical navigation
server, _ := doc.Section("server")
timeout, _ := server.GetInt("timeout")
# Features
- Read-only parser (no marshaling/writing)
- Parse from files
- Flat access via dot notation: doc.GetString("database.connection.timeout")
- Hierarchical navigation: doc.Section("database").Section("connection")
- List sections and keys
- Support for all TOML v1.0.0 data types
- Table arrays (array of tables)
- Nested sections
- Dotted keys
- Comprehensive error reporting with line/column information
# Data Types
The parser supports all TOML v1.0.0 data types:
- Strings (basic, multi-line, literal, multi-line literal)
- Integers (decimal, hex, octal, binary)
- Floats (standard, exponent, inf, nan)
- Booleans (true, false)
- Datetimes (offset, local date-time, local date, local time)
- Arrays
- Inline tables
# Access Patterns
Two complementary ways to access TOML data:
Flat Access (Dot Notation):
// Simple and direct for known paths
timeout, err := doc.GetInt("database.connection.timeout")
retries, err := doc.GetInt("database.connection.retries")
Hierarchical Navigation:
// Useful for exploring structure
db, err := doc.Section("database")
for _, name := range db.Sections() {
section, _ := db.Section(name)
fmt.Printf("Section: %s\n", section.Name())
for _, key := range section.Keys() {
fmt.Printf(" Key: %s\n", key)
}
}
# Table Arrays
TOML table arrays are accessed as slices of sections:
// [[fruits]]
// name = "apple"
// color = "red"
//
// [[fruits]]
// name = "banana"
// color = "yellow"
fruits, err := doc.TableArray("fruits")
for i, fruit := range fruits {
name, _ := fruit.GetString("name")
color, _ := fruit.GetString("color")
fmt.Printf("Fruit %d: %s (%s)\n", i, name, color)
}
# Configuration
Customize parser behavior with configuration:
cfg := toml.NewConfig().
SetStrict(true).
SetMaxDepth(100)
doc, err := toml.ParseWithConfig("config.toml", cfg)
Configuration options:
- Strict mode: Enforce strict TOML v1.0.0 compliance
- Allow duplicate keys: Last definition wins vs error
- Max depth: Limit nesting depth to prevent stack overflow
# Error Handling
The package provides detailed error information:
doc, err := toml.Parse("bad.toml")
if err != nil {
if parseErr, ok := err.(*toml.ParseError); ok {
fmt.Printf("Error at %s:%d:%d: %v\n",
parseErr.Path, parseErr.Line, parseErr.Col, parseErr.Err)
}
}
Error codes help categorize errors:
- ErrSyntax: Syntax errors in TOML
- ErrType: Type conversion errors
- ErrNotFound: Key or section not found
- ErrDuplicate: Duplicate key or table
- ErrInvalidPath: Invalid dot-notation path
- ErrIO: File I/O errors
- ErrInvalid: Invalid configuration
# Example
Complete example:
package main
import (
"fmt"
"log"
"tideland.dev/go/toml"
)
func main() {
// Parse TOML file
doc, err := toml.Parse("app.toml")
if err != nil {
log.Fatal(err)
}
// Access configuration values
appName, _ := doc.GetString("app.name")
version, _ := doc.GetString("app.version")
fmt.Printf("%s v%s\n", appName, version)
// Navigate sections
server, _ := doc.Section("server")
host, _ := server.GetString("host")
port, _ := server.GetInt("port")
fmt.Printf("Server: %s:%d\n", host, port)
// List all database connections
databases, _ := doc.TableArray("databases")
for i, db := range databases {
name, _ := db.GetString("name")
connStr, _ := db.GetString("connection_string")
fmt.Printf("DB %d: %s -> %s\n", i+1, name, connStr)
}
}
# TOML v1.0.0 Compliance
This package implements the complete TOML v1.0.0 specification:
- All data types (strings, integers, floats, booleans, datetimes, arrays, tables)
- String escape sequences
- Multi-line strings (basic and literal)
- Integer formats (decimal, hex, octal, binary)
- Float special values (inf, -inf, nan)
- Datetime formats (offset, local)
- Arrays (homogeneous and mixed)
- Inline tables
- Tables and nested tables
- Dotted keys
- Array of tables (table arrays)
- Comments
# Design Philosophy
The package follows Tideland Go standards:
- Simple things should be simple (flat dot notation access)
- Complex things should be possible (hierarchical navigation)
- Clear error messages with context
- Type safety with explicit conversions
- Comprehensive testing (>90% coverage)
- No external dependencies for core functionality
See the test files for more examples and usage patterns.
*/
package toml