Skip to content

Commit

Permalink
docs(examples): make examples more interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoksr committed Aug 11, 2024
1 parent dde5f6f commit eacac4e
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 183 deletions.
7 changes: 0 additions & 7 deletions examples/complex/go.mod

This file was deleted.

8 changes: 0 additions & 8 deletions examples/complex/go.sum

This file was deleted.

104 changes: 57 additions & 47 deletions examples/complex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"log"
"os"
"time"

"github.com/nikoksr/konfetty"
"github.com/nikoksr/konfetty/examples"
)

type SmartHomeConfig struct {
Expand Down Expand Up @@ -42,7 +44,7 @@ type ThermostatDevice struct {

type RoomConfig struct {
Name string
Devices []interface{} // Can be LightDevice or ThermostatDevice
Devices []any // Can be LightDevice or ThermostatDevice
}

type RoutineConfig struct {
Expand All @@ -55,7 +57,7 @@ type RoutineConfig struct {
type Action struct {
DeviceName string
Command string
Value interface{}
Value any
}

func main() {
Expand All @@ -68,7 +70,7 @@ func main() {
Rooms: []RoomConfig{
{
Name: "Living Room",
Devices: []interface{}{
Devices: []any{
LightDevice{
BaseDevice: BaseDevice{Name: "Main Light", Type: "light"},
Brightness: 80,
Expand Down Expand Up @@ -144,54 +146,62 @@ func main() {
log.Fatalf("Error building config: %v", err)
}

// Use the final config as needed...

examples.PrettyPrint(os.Stdout, cfg)

// The processed config would look like this:
//
// SmartHomeConfig {
// General: GeneralConfig {
// HomeName: "My Smart Home", // Kept original value
// OwnerName: "John Doe", // Kept original value
// TimeZone: "UTC", // Applied from defaults
// EnergyMode: "balanced", // Applied from defaults
// MaintenanceInterval: 720h0m0s, // Applied from defaults (30 days)
// },
// Rooms: []RoomConfig {
// {
// "General": {
// "HomeName": "My Smart Home", // Kept original value
// "OwnerName": "John Doe", // Kept original value
// "TimeZone": "UTC", // Applied from defaults
// "EnergyMode": "balanced", // Applied from defaults
// "MaintenanceInterval": 2592000000000000 // Applied from defaults (30 days in nanoseconds)
// },
// "Rooms": [
// {
// "Name": "Living Room", // Kept original value
// "Devices": [
// {
// Name: "Living Room", // Kept original value
// Devices: []any {
// LightDevice {
// BaseDevice: BaseDevice {
// Name: "Main Light", // Kept original value
// Type: "light", // Applied from LightDevice.BaseDevice default
// Enabled: false, // Applied from BaseDevice default
// Location: "Unknown", // Applied from BaseDevice default
// },
// Brightness: 80, // Kept original value
// ColorTemp: 3000, // Applied from LightDevice default
// },
// ThermostatDevice{
// BaseDevice: BaseDevice{
// Name: "AC", // Kept original value
// Location: "Unknown", // Applied from BaseDevice default
// Type: "thermostat", // Applied from ThermostatDevice.BaseDevice default
// Enabled: true, // Applied from ThermostatDevice.BaseDevice default
// },
// TargetTemp: 22.0, // Applied from ThermostatDevice default
// Mode: "auto", // Applied from ThermostatDevice default
// },
// },
// "Name": "Main Light", // Kept original value
// "Type": "light", // Applied from LightDevice default
// "Enabled": false, // Applied from BaseDevice default
// "Location": "Unknown", // Applied from BaseDevice default
// "Brightness": 80, // Kept original value
// "ColorTemp": 3000 // Applied from LightDevice default
// },
// },
// Routines: []RoutineConfig{
// {
// Name: "Morning Routine",
// StartTime: "07:00",
// Days: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"},
// Actions: []Action{
// {DeviceName: "Main Light", Command: "setBrightness", Value: 100},
// },
// },
// },
// "Name": "AC", // Kept original value
// "Type": "thermostat", // Applied from ThermostatDevice default
// "Enabled": true, // Applied from ThermostatDevice default
// "Location": "Unknown", // Applied from BaseDevice default
// "TargetTemp": 22, // Applied from ThermostatDevice default
// "Mode": "auto" // Applied from ThermostatDevice default
// }
// ]
// }
// ],
// "Routines": [
// {
// "Name": "Morning Routine", // Kept original value
// "StartTime": "07:00", // Kept original value
// "Days": [
// "Monday",
// "Tuesday",
// "Wednesday",
// "Thursday",
// "Friday"
// ], // Kept original values
// "Actions": [
// {
// "DeviceName": "Main Light", // Kept original value
// "Command": "setBrightness", // Kept original value
// "Value": 100 // Kept original value
// }
// ]
// }
// ]
// }

// Use processedCfg as needed...
}
13 changes: 10 additions & 3 deletions examples/viper/go.mod → examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
module github.com/nikoksr/konfetty/examples/viper
module github.com/nikoksr/konfetty/examples

go 1.22.5

replace github.com/nikoksr/konfetty => ../..
replace github.com/nikoksr/konfetty => ..

require (
github.com/knadh/koanf/parsers/yaml v0.1.0
github.com/knadh/koanf/providers/file v1.1.0
github.com/knadh/koanf/v2 v2.1.1
github.com/nikoksr/konfetty v0.0.0-00010101000000-000000000000
github.com/spf13/viper v1.19.0
)

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -25,7 +32,7 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
18 changes: 16 additions & 2 deletions examples/viper/go.sum → examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,32 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c=
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w=
github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY=
github.com/knadh/koanf/providers/file v1.1.0 h1:MTjA+gRrVl1zqgetEAIaXHqYje0XSosxSiMD4/7kz0o=
github.com/knadh/koanf/providers/file v1.1.0/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI=
github.com/knadh/koanf/v2 v2.1.1 h1:/R8eXqasSTsmDCsAyYj+81Wteg8AqrV9CP6gvsTsOmM=
github.com/knadh/koanf/v2 v2.1.1/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -57,8 +71,8 @@ go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
26 changes: 0 additions & 26 deletions examples/koanf/go.mod

This file was deleted.

37 changes: 0 additions & 37 deletions examples/koanf/go.sum

This file was deleted.

24 changes: 14 additions & 10 deletions examples/koanf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package main
import (
"errors"
"log"
"os"

"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"

"github.com/nikoksr/konfetty"
"github.com/nikoksr/konfetty/examples"
)

type DatabaseConfig struct {
Expand Down Expand Up @@ -39,7 +41,7 @@ func main() {
//

k := koanf.New(".")
if err := k.Load(file.Provider("../app.config.yaml"), yaml.Parser()); err != nil {
if err := k.Load(file.Provider("./app.config.yaml"), yaml.Parser()); err != nil {
log.Fatalf("Error loading config: %v", err)
}

Expand Down Expand Up @@ -82,20 +84,22 @@ func main() {

// Use the final config as needed...

examples.PrettyPrint(os.Stdout, cfg)

// The final config would look like this:
//
// {
// {
// "Database": {
// "Host": "super-secret-database.myapp.com",
// "Port": 5555,
// "Username": "myuser",
// "Password": "mypassword"
// "Host": "super-secret-database.myapp.com", // Kept original value, loaded by Koanf
// "Port": 5555, // Kept original value, loaded by Koanf
// "Username": "myuser", // Kept original value, loaded by Koanf
// "Password": "mypassword" // Kept original value, loaded by Koanf
// },
// "Server": {
// "Name": "Procrastination Station",
// "Host": "localhost",
// "Port": 8080
// "Name": "Procrastination Station", // Set by transformer function
// "Host": "localhost", // Kept original value, loaded by Koanf
// "Port": 8080 // Applied from defaults
// },
// "LogLevel": "info"
// "LogLevel": "info" // Applied from defaults
// }
}
7 changes: 0 additions & 7 deletions examples/simple/go.mod

This file was deleted.

8 changes: 0 additions & 8 deletions examples/simple/go.sum

This file was deleted.

Loading

0 comments on commit eacac4e

Please sign in to comment.