Giter VIP home page Giter VIP logo

overwrite's Introduction

Go overwrite

GoDoc License: MIT Go Report Card

Currently, overwrite requires Go version 1.13 or greater.

This repository contains a library that enable caller to setup two structs of the same type and copy only tagged values between then.

Possible use cases are:

  • Dynamically set up a struct based on input files
  • Overloading secret configuration in environments where secrets are stored outside repo, escpecially useful for docker and kubernetes environments.

Usage

import "github.com/phelian/overwrite"

type Config struct {
    Username string
    Password string `overwrite:"true"`
    Driver string `overwrite:"true,omitempty"`
}

func main() {
    cfg := &Config{
        Username: "bernie",
        Driver: "postgres",
    }

    secretCfg := Config {
        Password: "feelthe",
    }

    if err := overwrite.Do(cfg, secretCfg); err != nil {
        panic(err)
    }
}

The result will be that cfg be a pointer to a Config struct with contents

{
    Username: "bernie",
    Password: "feelthe",
    Driver: "postgres",
}

Do

Do copies tagged fields of arguments into

dst needs to be a pointer to same type of struct that src is src needs to be passed as value and not a pointer

Do traverses the value src recursively. If an encountered field is tagged to overwrite it tries to copy the value of that field into the dst counterpart field.

Errors

Error Description
ErrSrcNil Returned when input src is nil
ErrDstNil Returned when input dst is nil
ErrDstNotPtr Returned when input dst is not pointer to struct
ErrSrcNotStruct Returned when input src is not struct type
ErrNotSameType Returned when the input dst and src are not pointers or and static copy of same type of struct
ErrCannotSetField Returned when a tag have been set on a field that cannot be set, for instance not exported
ErrTagValueWrong Wrong input in tag, see Tags section for possible values allowed

Tags

`overwrite:"<true/false>[,omitempty]"`

Possible arguments for tag are boolean "true" or "false" with possibility to add ",omitempty" in the end.

Tag Value Description
true Overwrite value from src struct into dst struct
false In effect redundant and same as not setting tag
,omitempty Do not overwrite if source value is empty

Compatable types

There types can be overwritten, others will be silently ignored

  • int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64
  • string
  • boolean
  • float32, float64 Also
  • arrays, slices, maps and structs containing compatable types

Up next

  • Throw error on unsupported types with tags
  • Add tag to allow for configuration to only overwrite is destination struct is empty

overwrite's People

Contributors

phelian avatar

Watchers

 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.