Git Product home page Git Product logo

go-struct-tag-list's Introduction

Go Report Card Build Status codecov GoDoc

Go Struct Tags to Comma Separated List

This go library returns a comma separated list of tags from pure structs.

Idea

The idea of this lib is to simplify SQL queries without using an ORM.

Example:

type Clients struct {
	Name   string `db:"username" json:"Username"`
	City   string `db:"location" json:"City"`
	Age    int    `db:"age" json:"Age"`
	Gender string `db:"mf" json:"Gender"`
	Car    string `db:"auto" json:"Auto"`
	}

Instead of manually creating an SQL Query like "SELECT Name, City, Age, Gender, Car FROM..." the query can be written this way:

fmt.Sprintf("SELECT %v WHERE %v = @p1", list.StructTagList(Clients{},"db",true), field), param)

Whenever the struct changes automatically also the Query will change.

Usage

import "github.com/pitw/go-struct-tag-list/list"

The list package needs 3 params:

Param Type Example Note
Struct struct{} Clients{} The target struct
Struct Tag string "db" The struct tag which should be exported as list
Check bool true Checks if all fields in struct have this tag

The list package returns a comma separated list as string and errors.

type Clients struct {
	Name   string `db:"username" json:"Username"`
	City   string `db:"location" json:"City"`
	Age    int    `db:"age" json:"Age"`
	Gender string `db:"mf" json:"Gender"`
	Car    string `db:"auto" json:"Auto"`
}

v, err := list.StructTagList(Clients{}, "db", true)
fmt.Print(v)   // returns username,location,age,mf,auto
fmt.Print(err)  // returns nil


v, err := list.StructTagTypeList(Clients{}, "db", true)
  

go-struct-tag-list's People

Contributors

pitw avatar

Watchers

 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.