Giter Site home page Giter Site logo

remapper's Introduction

remapper

remapper provides some convenience functions to remap data from one type to another

package main

import (
    "github.com/plandem/remapper"
    "log"
)

type MyStruct struct {
    IntVal   int        `db:"int_val"`
    UintVal  uint    `db:"uint_val"`
    StrVal   string    `db:"str_val"`
    FloatVal float64    `db:"float_val"`
    BoolVal  bool    `db:"bool_val"`
}

func main() {
    //named-typed-slice and struct with tags
    mapper, err := remapper.New(&MyStruct{}, remapper.Slice([]string{}, []string{"int_val","uint_val","str_val","float_val","bool_val"}), "db")

    if err != nil {
        panic(err)
    }

    // slice -> struct
    s, err := mapper.Map(&[]string{
        "-1",
        "1",
        "test string",
        "1.2345",
        "true",
    })

    log.Printf("struct: %+v, error: %v", s, err)

    // struct -> slice
    arr, err := mapper.Map(s)
    log.Printf("arr: %+v, error: %v", arr, err)

    //named-untyped-slice and struct with manual mapping
    mapper, err = remapper.New(&MyStruct{}, remapper.Slice([]interface{}{}, []string{"int_val","uint_val","str_val","float_val","bool_val"}), map[string]string{
        "IntVal":   "int_val",
        "UintVal":  "uint_val",
        "StrVal":   "str_val",
        "FloatVal": "float_val",
        "BoolVal":  "bool_val",
    })
    
    if err != nil {
        panic(err)
    }

    // slice -> struct
    s, err = mapper.Map([]interface{}{
        -1,
        1,
        "test string",
        1.2345,
        true,
    })

    log.Printf("struct: %+v, error: %v", s, err)

    // struct -> slice
    arr, err = mapper.Map(s)
    log.Printf("arr: %+v, error: %v", arr, err)
}

Documentation can be found at http://godoc.org/github.com/plandem/remapper

remapper's People

Contributors

plandem avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

luxms

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.