dl

package module
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 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 and modified to meet the requirements of this project.

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, using the -f flag to specify the file path with filename or directory:

dl -f ./demo.go

This will generate a Default() error method in your struct that initializes the fields with the specified default values.

Below is the generated code example with Demo struct:

// Default loads default values for Demo
func (obj *Demo) Default() error {
	obj.Name = "demo"
	return nil
}
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 Object added in v0.0.9

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

Object creates a value from a 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
cmd module
gen module

Jump to

Keyboard shortcuts

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