Documentation ¶
Overview ¶
Provides a simple way to get all the options you typically expect to be set out of the box in goschtalt.
Why not include this automatically? ¶
- Due to separate packages for the adapters that depends on goschtalt, goschtalt can't also depend upon the adapters, so a 3rd package is needed.
- If you don't want these values, no problem, simply don't include this package.
- Testing is easier with no default values.
What is included? ¶
- goschtalt.AutoCompile() - no need to compile explicitly
- goschtalt.ExpandEnv() - environment variables in the form of ${var} are replaced
- goschtalt.IncludeOrigins() - the origin of the configuration is included
- goschtalt.FormatAs("yml") - the output format used for the configuration is yml
- goschtalt.Strictness(SUBSET) - the configuration can be a subset, but not contain extra things
- adapter.BoolUnmarshal() / adapter.MarshalBool() - convert bool to/from string
- adapter.DurationUnmarshal() / adapter.MarshalDuration() - convert time.Duration to/from string
- adapter.FloatUnmarshal() / adapter.MarshalFloat() - convert float32/float64 to/from string
- adapter.IntUnmarshal() / adapter.MarshalInt() - convert int/int8/int16/int32/int64 to/from string
- adapter.TextUnmarshal() / adapter.MarshalText() - convert objects that implement UnmarshalText/TextMarshal to/from string
- adapter.TimeUnmarshal() / adapter.MarshalTime() - convert time.Time to/from string (in RFC3339 form)
- adapter.UintUnmarshal() / adapter.MarshalUint() - convert uint/uint8/uint16/uint32/uint64 to/from string
Usage ¶
Add the following lines to the import list & everything is automatically ready to go.
import ( _ "github.com/goschtalt/goschtalt/pkg/typical" _ "github.com/goschtalt/yaml-decoder" _ "github.com/goschtalt/yaml-encoder" )
Example ¶
// SPDX-FileCopyrightText: 2023 Weston Schmidt <weston_schmidt@alumni.purdue.edu> // SPDX-License-Identifier: Apache-2.0 package main import ( "fmt" // Uncomment below use a real yaml decoder/encoder. By not using these // in the goschtalt packages they aren't included in the dependencies. // This allows you to more easily control what decoders you want and also // allows easy to replacement in the future if a package becomes // unmaintained. //_ "github.com/goschtalt/yaml-decoder" //_ "github.com/goschtalt/yaml-encoder" "github.com/goschtalt/goschtalt" "github.com/goschtalt/goschtalt/pkg/decoder" "github.com/goschtalt/goschtalt/pkg/meta" _ "github.com/goschtalt/goschtalt/pkg/typical" ) type Config struct { Name string } func main() { gs, err := goschtalt.New( goschtalt.ConfigIs("two_words"), goschtalt.AddValue("built-in", goschtalt.Root, Config{ Name: "app_name", }, goschtalt.AsDefault(), ), onlyNeededForExample(), ) if err != nil { panic(err) } c, err := goschtalt.Unmarshal[Config](gs, goschtalt.Root) if err != nil { panic(err) } fmt.Printf("Name: %q\n", c.Name) } // -- Normally the code below isn't needed. ------------------------------------ type fake struct{} func (*fake) EncodeExtended(meta.Object) ([]byte, error) { return nil, nil } func (*fake) Encode(v any) ([]byte, error) { return nil, nil } func (*fake) Decode(decoder.Context, []byte, *meta.Object) error { return nil } func (*fake) Extensions() []string { return []string{"yml", "yaml"} } func onlyNeededForExample() goschtalt.Option { // Normally these are provided by yaml-decoder and yaml-encoder. return goschtalt.Options( goschtalt.WithDecoder(&fake{}), goschtalt.WithEncoder(&fake{}), ) }
Output: Name: "app_name"
Click to show internal directories.
Click to hide internal directories.