structology

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: Apache-2.0 Imports: 5 Imported by: 23

README

structology - State tracker for golang struct

GoReportCard GoDoc

This library is compatible with Go 1.17+

Please refer to CHANGELOG.md if you encounter breaking changes.

Motivation

This project defines struct field set marker to distinctively identify input state, specifically for 'empty', 'nil', 'has been set' state. This is critical for update/patch operation with large struct where only small subset if defined, with presence marker allowing you handling user input effectively, ensuring data integrity, and improving the security of applications

Initially we have a few project holding marker abstraction to finally move it to this project.

Usage

type (
    HasEntity struct {
	    Id     bool
        Name   bool
        Active bool
	}
     Entity struct {
        Id     int
        Name   string
        Active bool
        Has    *EntityHas `setMarker:"true"`
    }
)

    var entity *Entity

	....// load entity and set all present fields with marker.Set(...)

	
    marker, err := NewMarker()
    if err != nil {
        log.Fatal(err)
    }
    isIdSet := marker.IsSet(marker.Index("Id"))
	fmt.Printf("is ID set : %v\n", isIdSet)
	
}

Documentation

Index

Constants

View Source
const (
	//SetMarkerTag defines set marker tag
	SetMarkerTag = "setMarker"
)

Variables

This section is empty.

Functions

func GenMarkerFields

func GenMarkerFields(t reflect.Type) []reflect.StructField

GenMarkerFields generate marker struct fields

func IsSetMarker

func IsSetMarker(tag reflect.StructTag) bool

Types

type Marker

type Marker struct {
	// contains filtered or unexported fields
}

Marker field set marker

func NewMarker

func NewMarker(t reflect.Type, opts ...Option) (*Marker, error)

NewMarker returns new struct field set marker

func (*Marker) CanUseHolder

func (p *Marker) CanUseHolder(ptr unsafe.Pointer) bool

func (*Marker) Index

func (p *Marker) Index(name string) int

Index returns mapped field index or -1

func (*Marker) IsSet

func (p *Marker) IsSet(ptr unsafe.Pointer, index int) bool

IsSet returns true if field has been set

func (*Marker) Set

func (p *Marker) Set(ptr unsafe.Pointer, index int, flag bool) error

Set sets field marker

func (*Marker) SetAll

func (p *Marker) SetAll(ptr unsafe.Pointer, flag bool) error

SetAll sets all marker field with supplied flag

type Option

type Option func(m *Marker)

Option marker option

func WithIndex

func WithIndex(index map[string]int) Option

WithIndex filed name to index mapping

func WithNoStrict added in v0.2.0

func WithNoStrict(flag bool) Option

WithNoStrict create no strict option

type Options

type Options []Option

Options represents marker option

func (Options) Apply

func (o Options) Apply(m *Marker)

Apply applies options

Jump to

Keyboard shortcuts

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