dl

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2024 License: MIT Imports: 7 Imported by: 0

README

DL(Default Loader)

Overview

DL (Default Loader) is a tool designed to generate and assign default values to fields within Go structs based on tags. This utility allows you to specify default values for your struct fields using a simple tag syntax, making it easier to initialize structs with predefined values without having to explicitly set them in your code. defaults.go is forked from creasty/defaults

Features

  • Tag-based Default Values: Use the default tag to set default values for struct fields.
  • Easy Integration: Quickly generate methods to load default values into your structs.
  • Efficient Initialization: Simplifies the initialization process of complex structs by automatically setting default values.

Installation

To use DL (Default Loader), first install the tool via:

go install github.com/godcong/dl/cmd@latest

Usage

Step 1: Define Struct with Default Tags

Add the default tag to your struct fields to specify their default values:

// example: demo.go
type Demo struct {
Name string `default:"demo"`
}
Step 2: Generate Default Value Loading Method

Run DL to generate the necessary loading method for your struct:

dl -f ./demo.go
Step 3: Load Default Values

In your code, use dl.Load() to populate your struct with the default values:

func main() {
demo := &Demo{}
if err := dl.Load(demo); err != nil {
panic(err)
} 
// Now 'demo' has its fields initialized with default values. 
}

Documentation

Overview

Package dl for Default Loader

Package dl for Default Loader

Package dl for Default Loader

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidType = errors.New("empty")

Functions

func CanUpdate added in v0.0.7

func CanUpdate(v interface{}) bool

CanUpdate returns true when the given value is an initial value of its type

func InvalidTypeError

func InvalidTypeError(typeString string) error

func Load

func Load[T any](ptr *T) error

Load initializes members in a struct referenced by a pointer. Maps and slices are initialized by `make` and other primitive types are set with default values. `ptr` should be a struct pointer

func LoadInterface

func LoadInterface[P any](ptr any, arg P) (bool, error)

LoadInterface initializes members in a struct referenced by a pointer. Maps and slices are initialized by `make` and other primitive types are set with default values. `ptr` should be a struct pointer

func LoadStruct

func LoadStruct(ptr any) error

LoadStruct initializes members in a struct referenced by a pointer. Maps and slices are initialized by `make` and other primitive types are set with default values. `ptr` should be a struct pointer

func LoadWithOption added in v0.0.7

func LoadWithOption[T any, P any](ptr *T, arg P) error

LoadWithOption initializes members in a struct referenced by a pointer. Maps and slices are initialized by `make` and other primitive types are set with default values. `ptr` should be a struct pointer

func MustLoad added in v0.0.7

func MustLoad[T any](ptr *T)

MustLoad initializes members in a struct referenced by a pointer. Maps and slices are initialized by `make` and other primitive types are set with default values. `ptr` should be a struct pointer

func Pointer

func Pointer[T any](v T) *T

Pointer creates a pointer to a value.

Types

type DefaultLoader

type DefaultLoader interface {
	Default() error
}

DefaultLoader is an interface that can be implemented by structs to customize the default

type DefaultLoaderFunc added in v0.0.7

type DefaultLoaderFunc[T any] func(*T) error

DefaultLoaderFunc is a function type that defines a function to load default values into a struct referenced by a pointer.

type DefaultOptionLoader added in v0.0.7

type DefaultOptionLoader[P any] interface {
	Default(P) error
}

DefaultOptionLoader is an interface that specifies a method to load default values into a struct with a parameter.

type DefaultOptionLoaderFunc added in v0.0.7

type DefaultOptionLoaderFunc[T any, P any] func(*T, P) error

DefaultOptionLoaderFunc is a function type that defines a function to load default values into a struct with a parameter.

Directories

Path Synopsis
Package main
Package main
Package gen for Default Loader
Package gen for Default Loader
internal
io
Package io for Default Loader
Package io for Default Loader
shell
Package shell for Default Loader
Package shell for Default Loader
tpl
Package tpl for Default Loader
Package tpl for Default Loader

Jump to

Keyboard shortcuts

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