Documentation
¶
Overview ¶
Package yaml provides YAML marshal/unmarshal functions with custom naming conventions.
Package yaml provides YAML marshal/unmarshal functions with custom naming conventions.
Package yaml provides YAML marshal/unmarshal functions with custom naming conventions.
Example ¶
Example demonstrates basic usage of MarshalKebab and UnmarshalKebab.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/yaml"
)
func main() {
type Config struct {
ServerName string
ListenPort int
EnableTLS bool
}
cfg := Config{
ServerName: "my-server",
ListenPort: 8080,
EnableTLS: true,
}
// Marshal to YAML with kebab-case
data, err := yaml.MarshalKebab(cfg)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
// Unmarshal from YAML with kebab-case
var loaded Config
if err := yaml.UnmarshalKebab(data, &loaded); err != nil {
log.Fatal(err)
}
fmt.Printf("Loaded: %+v\n", loaded)
}
Index ¶
- func MarshalKebab(v interface{}) ([]byte, error)
- func MarshalKebabToFile(filename string, v interface{}) error
- func MarshalSnake(v interface{}) ([]byte, error)
- func MarshalSnakeToFile(filename string, v interface{}) error
- func UnmarshalKebab(data []byte, v interface{}) error
- func UnmarshalKebabFromFile(filename string, v interface{}) error
- func UnmarshalSnake(data []byte, v interface{}) error
- func UnmarshalSnakeFromFile(filename string, v interface{}) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalKebab ¶
MarshalKebab marshals v to YAML bytes using kebab-case for field names that don't have explicit yaml tags. Fields with explicit yaml:"name" tags will use the tag value.
This function uses reflection to transform struct field names to kebab-case before marshaling. Only exported fields are processed (standard Go behavior).
Example ¶
ExampleMarshalKebab demonstrates marshaling with explicit tags.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/yaml"
)
func main() {
type Config struct {
ServerName string `yaml:"custom_server_name"`
ListenPort int // Will use kebab-case: listen-port
EnableTLS bool `yaml:"tls_enabled"`
}
cfg := Config{
ServerName: "test-server",
ListenPort: 8080,
EnableTLS: true,
}
data, err := yaml.MarshalKebab(cfg)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
func MarshalKebabToFile ¶
MarshalKebabToFile marshals v to a YAML file using kebab-case naming convention.
Example ¶
ExampleMarshalKebabToFile demonstrates writing YAML to a file.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/yaml"
)
func main() {
type Config struct {
ServerName string
ListenPort int
}
cfg := Config{
ServerName: "file-server",
ListenPort: 9090,
}
// Write to file
if err := yaml.MarshalKebabToFile("/tmp/config.yaml", cfg); err != nil {
log.Fatal(err)
}
fmt.Println("Config written to /tmp/config.yaml")
}
func MarshalSnake ¶
MarshalSnake marshals v to YAML bytes using snake_case for field names that don't have explicit yaml tags. Fields with explicit yaml:"name" tags will use the tag value.
This function uses reflection to transform struct field names to snake_case before marshaling. Only exported fields are processed (standard Go behavior).
Example ¶
ExampleMarshalSnake demonstrates basic usage of MarshalSnake.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/yaml"
)
func main() {
type Config struct {
ServerName string
ListenPort int
EnableTLS bool
}
cfg := Config{
ServerName: "my-server",
ListenPort: 8080,
EnableTLS: true,
}
data, err := yaml.MarshalSnake(cfg)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
func MarshalSnakeToFile ¶
MarshalSnakeToFile marshals v to a YAML file using snake_case naming convention.
Example ¶
ExampleMarshalSnakeToFile demonstrates writing YAML with snake_case to a file.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/yaml"
)
func main() {
type Config struct {
ServerName string
ListenPort int
}
cfg := Config{
ServerName: "file-server",
ListenPort: 9090,
}
// Write to file
if err := yaml.MarshalSnakeToFile("/tmp/config-snake.yaml", cfg); err != nil {
log.Fatal(err)
}
fmt.Println("Config written to /tmp/config-snake.yaml")
}
func UnmarshalKebab ¶
UnmarshalKebab unmarshals YAML bytes into v, expecting kebab-case field names for fields without explicit yaml tags. Fields with explicit yaml:"name" tags will use the tag value.
This function uses reflection to transform struct field names to kebab-case before unmarshaling. Only exported fields are processed (standard Go behavior).
Example ¶
ExampleUnmarshalKebab demonstrates unmarshaling with nested structs.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/yaml"
)
func main() {
type ServerSettings struct {
HostName string
ListenPort int
}
type DatabaseConfig struct {
ConnectionString string
MaxConnections int `yaml:"max_conn"`
}
type AppConfig struct {
ServerSettings ServerSettings
DatabaseConfig DatabaseConfig
}
yamlData := []byte(`
server-settings:
host-name: localhost
listen-port: 8080
database-config:
connection-string: postgres://localhost
max_conn: 10
`)
var cfg AppConfig
if err := yaml.UnmarshalKebab(yamlData, &cfg); err != nil {
log.Fatal(err)
}
fmt.Printf("Server: %s:%d\n", cfg.ServerSettings.HostName, cfg.ServerSettings.ListenPort)
fmt.Printf("Database: %s (max %d connections)\n",
cfg.DatabaseConfig.ConnectionString,
cfg.DatabaseConfig.MaxConnections)
}
func UnmarshalKebabFromFile ¶
UnmarshalKebabFromFile unmarshals YAML from a file into v using kebab-case naming convention.
Example ¶
ExampleUnmarshalKebabFromFile demonstrates reading YAML from a file.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/yaml"
)
func main() {
type Config struct {
ServerName string
ListenPort int
}
var cfg Config
if err := yaml.UnmarshalKebabFromFile("/tmp/config.yaml", &cfg); err != nil {
log.Fatal(err)
}
fmt.Printf("Loaded config: %+v\n", cfg)
}
func UnmarshalSnake ¶
UnmarshalSnake unmarshals YAML bytes into v, expecting snake_case field names for fields without explicit yaml tags. Fields with explicit yaml:"name" tags will use the tag value.
This function uses reflection to transform struct field names to snake_case before unmarshaling. Only exported fields are processed (standard Go behavior).
Example ¶
ExampleUnmarshalSnake demonstrates unmarshaling with snake_case.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/yaml"
)
func main() {
type Config struct {
ServerName string
ListenPort int
EnableTLS bool
}
yamlData := []byte(`
server_name: test-server
listen_port: 8080
enable_tls: true
`)
var cfg Config
if err := yaml.UnmarshalSnake(yamlData, &cfg); err != nil {
log.Fatal(err)
}
fmt.Printf("Server: %s:%d (TLS: %v)\n", cfg.ServerName, cfg.ListenPort, cfg.EnableTLS)
}
func UnmarshalSnakeFromFile ¶
UnmarshalSnakeFromFile unmarshals YAML from a file into v using snake_case naming convention.
Example ¶
ExampleUnmarshalSnakeFromFile demonstrates reading YAML with snake_case from a file.
package main
import (
"fmt"
"log"
"codeberg.org/basvanbeek/run/pkg/yaml"
)
func main() {
type Config struct {
ServerName string
ListenPort int
}
var cfg Config
if err := yaml.UnmarshalSnakeFromFile("/tmp/config-snake.yaml", &cfg); err != nil {
log.Fatal(err)
}
fmt.Printf("Loaded config: %+v\n", cfg)
}
Types ¶
This section is empty.