upstruct

package module
v0.0.0-...-2338416 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

README

upstruct GoDoc

upstruct is an utility package to update a golang struct with another one

This is a fork of upstruct, go show some love if you like it.

Install

go get github.com/LordMik3/upstruct

Example Update

import "github.com/hackirby/upstruct"

type User struct {
    Username string
    Password string
}

type UserPatch struct {
    Username string
    Password string
}

var user = User{
    Username: "user",
    Password: "password",
}

var userPatch = UserPatch{
    Username: "newuser",
    Password: "newpassword",
}

func main() {
    upstruct.Update(&user, userPatch)
}

Example UpdateFn

import (
    "reflect"

    "github.com/hackirby/upstruct"
)



type address struct {
    StreetNumber uint16
    StreetName   string
}

type target struct {
    Name    string
    Age     uint8
    Email   sql.NullString
    Address address
}

type update struct {
    Name    string
    Age     uint8
    Email   string
    Address address
}

var target := target{
        Name: "upstruct",
        Age:  18, // legal
        Email: sql.NullString{
            String: "",
            Valid:  false,
        },
        Address: address{
            StreetNumber: 105,
            StreetName:   "cpu street",
        },
    }

var update := update{
        Name:  "up struct",
        Email: "test@gmail.com",
        Address: address{
            StreetName: "gpu street",
        },
    }

func main(){
    upstruct.UpdateFn(&target, &update, func(f1, f2 *structs.Field) {
        if reflect.TypeOf(f1.Value()).String() == "sql.NullString" {
            f1.Set(sql.NullString{
                String: f2.Value().(string),
                Valid:  true,
            })
        }
    })
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Update

func Update(target any, update any, fieldHandlers ...UpdateStructOption) error

Types

type DifferentTypesOption

type DifferentTypesOption struct {
	TargetType string
	UpdateType string
}

func (DifferentTypesOption) IsConditionMet

func (d DifferentTypesOption) IsConditionMet(targetValue interface{}, updateValue interface{}) bool

func (DifferentTypesOption) IsValid

func (d DifferentTypesOption) IsValid() (bool, error)

type OptionHandler

type OptionHandler func(target, update *structs.Field)

type SameKindOption

type SameKindOption struct {
	Kind reflect.Kind
}

func (SameKindOption) IsConditionMet

func (d SameKindOption) IsConditionMet(targetValue interface{}, updateValue interface{}) bool

func (SameKindOption) IsValid

func (s SameKindOption) IsValid() (bool, error)

type UpdateOption

type UpdateOption interface {
	IsValid() (bool, error)
	IsConditionMet(targetValue interface{}, updateValue interface{}) bool
}

type UpdateStructOption

type UpdateStructOption struct {
	Option  UpdateOption
	Handler OptionHandler
}

Jump to

Keyboard shortcuts

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