upstruct

package module
v0.0.0-...-2cc65b3 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: GPL-3.0 Imports: 2 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) error

func UpdateFn

func UpdateFn(target any, update any, fieldHandler ...func(*structs.Field, *structs.Field)) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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