Giter VIP home page Giter VIP logo

go-patch's Introduction

go-patch

With go-patch you can selectively update golang structs with values from other structs.

API

func Struct(dstStruct, srcStruct interface{}) (bool, error)

patch.Struct updates a destination structure in-place with the same name fields from a supplied patch struct. Fields get matched by their names (case sensitive). Thus patch fields with a name not present in the destination structure are ignored. Zero-value and unexported fields in a patch struct are also ignored.

Notice, both the destination and patch structure can have embedded structs in them.

Example

import "github.com/aglyzov/go-patch"

type Employee struct {
    FirstName string
    LastName  string
    Salary    int
    Extra     string
}
type Patch struct {
    FirstName  string   // names and types should match
    LastName   *string  // however a patching field can also be a pointer 
    Salary     int      // only non-zero values are considered

    unexported bool     // unexported fields are ignored 
    Unknown    []byte   // fields not present in the target are also ignored
}

var e = Employee{
    FirstName: "Anakin",
    LastName:  "Skywalker",
    Salary:    123,
    Extra:     "unchanged",
}
var lastName = "Vader"
var p = Patch{
    FirstName:  "Darth",
    LastName:   &lastName, // pointer to a string
    Salary:     0,         // zero-value is ignored
    unexported: true,
    Unknown:    []byte("ignored"),
}
var changed, err = patch.Struct(&e, p)

// now `e` is {"Darth", "Vader", 123, "unchanged"}

go-patch's People

Contributors

aglyzov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.