Giter Site home page Giter Site logo

stringenum's Introduction

stringenum

A go tool to auto-generate serialization / validation methods for enum types aliasing string.

Features

  • Supports JSON, YAML serialization
  • Implements Validator interface
  • Does not support default values yet. But it has good fit with third-party default value modules like creasty/defaults because your types are basically a string!

Installation

You need to install stringenum to generate enum stub codes.

 $ go get github.com/therne/stringenum/...

Usage

On the top of your type definition sources, add go generate clause to generate stub codes with stringenum.

+ //go:generate stringenum Kind
package mytype

type Kind string

const (
    Apple  = Kind("apple")
    Google = Kind("google")
)

Then, run go generate to generate stub codes:

 $ go generate ./...

Generated Values and Methods

  • <Type>Values: A list of all available values in the enum.
  • <Type>FromString(string): Casts string into the enum. An error is returned if given string is not defined on the enum.
  • IsValid(): Returns false if the value is not defined on the enum.
  • Validate(): Returns an error if the value is not defined on the enum.
  • MarshalText / UnmarshalText: Implements encoding.TextMarshaler / TextUnmarshaler interface for JSON / YAML serialization.
  • String() : Casts the enum into a string. Implements fmt.Stringer interface.

License: MIT

stringenum's People

Contributors

therne avatar

Stargazers

Márk Bartos avatar Park, Beom-Jun avatar Serge Simard avatar Jinil Ha avatar Aaron Roh avatar Taewon Lee avatar Billo Park avatar  avatar Geon Kim avatar  avatar ByeongSu Hong avatar Chanhee Lee avatar wonkyun Lim avatar

Watchers

James Cloos 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.