typical

package
v0.25.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2024 License: Apache-2.0 Imports: 3 Imported by: 1

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?

  1. 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.
  2. If you don't want these values, no problem, simply don't include this package.
  3. Testing is easier with no default values.

What is included?

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"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL