enum

package module
v2.0.1+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2021 License: MIT Imports: 0 Imported by: 2

README

enum

MIT License GoDoc Go Report Card travis Status

TOC

Go Enum and its string representation lib

API

> test/Enum_test.go
////////////////////////////////////////////////////////////////////////////
// Porgram: Enum_test.go
// Purpose: Go Enum and its string representation lib demo
// Authors: Tong Sun (c) 2017-2021, All rights reserved
////////////////////////////////////////////////////////////////////////////

package test

import (
	"fmt"

	"github.com/suntong/enum"
)

var (
	example = enum.NewEnum()
	Alpha   = example.Iota("Alpha")
	Beta    = example.Iota("Beta")

	weekday = enum.NewEnum()
	Sunday  = weekday.Iota("Sunday")
	Monday  = weekday.Iota("Monday")

	wkday = enum.NewEnum()
	Suday = wkday.Iota("Su")
	Moday = wkday.Iota("Mo")
	Tuday = wkday.Iota("Tu")
	Weday = wkday.Iota("We")
	Thday = wkday.Iota("Th")
	Frday = wkday.Iota("Fr")
	Saday = wkday.Iota("Sa")

	wxType           = enum.NewEnum()
	MsgtypeText      = wxType.IotaAt("文本消息", 1)
	MsgtypeImage     = wxType.IotaAt("图片消息", 3)
	MsgtypeVoice     = wxType.IotaAt("语音消息", 34)
	MsgtypeSharecard = wxType.IotaAt("名片消息", 42)
	MsgtypeVideo     = wxType.IotaAt("视频消息", 43)
	MsgtypeEmoticon  = wxType.IotaAt("表情消息", 47)
	MsgtypeLocation  = wxType.IotaAt("地理位置消息", 48)
	MsgtypeApp       = wxType.IotaAt("APP消息", 49)
	MsgtypeVoipmsg   = wxType.IotaAt("VOIP消息", 50)
	MsgtypeSys       = wxType.IotaAt("系统消息", 10000)
)

// for standalone test, change package to main and the next func def to,
// func main() {
func Example_output() {
	fmt.Printf("%s\n", example.String(Alpha))
	fmt.Printf("%s\n", example.String(Beta))
	fmt.Println("=======")
	fmt.Printf("%d\t%d\n", Alpha, Alpha+1)
	fmt.Printf("%s\t%s\n", example.String(Beta-1), example.String(Alpha+1))
	fmt.Println("=======")
	if a, ok := example.Get("Alpha"); ok {
		fmt.Printf("%d: %s\n", a, example.String(a))
	}
	if b, ok := example.Get("Beta"); ok {
		fmt.Printf("%d: %s\n", b, example.String(b))
	}

	fmt.Printf("%d:%s\n", Sunday, weekday.String(Sunday))
	fmt.Printf("%d:%s\n", Monday, weekday.String(Monday))

	fmt.Println("=======")
	fmt.Printf("%s: %d\n", wkday.String(Suday), Suday)
	fmt.Printf("%s: %d\n", wkday.String(Moday), Moday)
	fmt.Printf("%s: %d\n", wkday.String(Moday+Thday), Frday)
	fmt.Printf("%s: %d\n", wkday.String(Saday), Weday*2)

	fmt.Println("=======")
	for _, wxt := range []int{
		MsgtypeText, MsgtypeImage, MsgtypeVoice, MsgtypeSharecard, MsgtypeVideo,
		MsgtypeEmoticon, MsgtypeLocation, MsgtypeApp, MsgtypeVoipmsg} {
		fmt.Printf("收到一条%s(type %d)\n", wxType.String(wxt), wxt)
	}
	fmt.Println("=======")
	for _, wxt := range []int{10000, 51} {
		wxtstr, ok := wxType.Exist(wxt)
		if ok {
			fmt.Printf("收到一条%s(type %d)\n", wxtstr, wxt)
		} else {
			fmt.Printf("收到一条未知消息(type %d):%s\n", wxt, wxtstr)
		}
	}
	fmt.Println("=======")
	for _, wxt := range []int{10000, 51} {
		wxtstr, ok := wxType.Exist(wxt)
		if !ok {
			wxtstr = "未知消息"
		}
		fmt.Printf("收到一条%s(type %d)\n", wxtstr, wxt)
	}
	// Output:
	// Alpha
	// Beta
	// =======
	// 0	1
	// Alpha	Beta
	// =======
	// 0: Alpha
	// 1: Beta
	// 0:Sunday
	// 1:Monday
	// =======
	// Su: 0
	// Mo: 1
	// Fr: 5
	// Sa: 6
	// =======
	// 收到一条文本消息(type 1)
	// 收到一条图片消息(type 3)
	// 收到一条语音消息(type 34)
	// 收到一条名片消息(type 42)
	// 收到一条视频消息(type 43)
	// 收到一条表情消息(type 47)
	// 收到一条地理位置消息(type 48)
	// 收到一条APP消息(type 49)
	// 收到一条VOIP消息(type 50)
	// =======
	// 收到一条系统消息(type 10000)
	// 收到一条未知消息(type 51):
	// =======
	// 收到一条系统消息(type 10000)
	// 收到一条未知消息(type 51)
}

Ref: API, v1

> Enum_v1_test.go
////////////////////////////////////////////////////////////////////////////
// Porgram: Enum_test.go
// Purpose: Go Enum and its string representation lib demo
// Authors: Tong Sun (c) 2017-2021, All rights reserved
////////////////////////////////////////////////////////////////////////////

package enum_test

import (
	"fmt"

	"gopkg.in/suntong/enum.v1"
)

var (
	// Note this is a v1 example, i.e.,
	// the enum variables are declared,
	// instead of defined with `enum.NewEnum()`
	example enum.Enum
	Alpha   = example.Iota("Alpha")
	Beta    = example.Iota("Beta")

	weekday enum.Enum
	Sunday  = weekday.Iota("Sunday")
	Monday  = weekday.Iota("Monday")

	wkday enum.Enum
	Suday = wkday.Iota("Su")
	Moday = wkday.Iota("Mo")
	Tuday = wkday.Iota("Tu")
	Weday = wkday.Iota("We")
	Thday = wkday.Iota("Th")
	Frday = wkday.Iota("Fr")
	Saday = wkday.Iota("Sa")
)

// for standalone test, change package to main and the next func def to,
// func main() {
func Example_output() {
	fmt.Printf("%s\n", example.String(Alpha))
	fmt.Printf("%s\n", example.String(Beta))
	fmt.Println("=======")
	fmt.Printf("%d\t%d\n", Alpha, Alpha+1)
	fmt.Printf("%s\t%s\n", example.String(Beta-1), example.String(Alpha+1))
	fmt.Println("=======")
	if a, ok := example.Get("Alpha"); ok {
		fmt.Printf("%d: %s\n", a, example.String(a))
	}
	if b, ok := example.Get("Beta"); ok {
		fmt.Printf("%d: %s\n", b, example.String(b))
	}

	fmt.Printf("%d:%s\n", Sunday, weekday.String(Sunday))
	fmt.Printf("%d:%s\n", Monday, weekday.String(Monday))
	fmt.Println("=======")

	fmt.Printf("%s: %d\n", wkday.String(Suday), Suday)
	fmt.Printf("%s: %d\n", wkday.String(Moday), Moday)
	fmt.Printf("%s: %d\n", wkday.String(Moday+Thday), Frday)
	fmt.Printf("%s: %d\n", wkday.String(Saday), Weday*2)
	// Output:
	// Alpha
	// Beta
	// =======
	// 0	1
	// Alpha	Beta
	// =======
	// 0: Alpha
	// 1: Beta
	// 0:Sunday
	// 1:Monday
	// =======
	// Su: 0
	// Mo: 1
	// Fr: 5
	// Sa: 6
}

Author(s) & Contributor(s)

Tong SUN
suntong from cpan.org

All patches welcome.

Documentation

Overview

Example (Output)

for standalone test, change package to main and the next func def to, func main() {

////////////////////////////////////////////////////////////////////////////
// Porgram: Enum_test.go
// Purpose: Go Enum and its string representation lib demo
// Authors: Tong Sun (c) 2017-2021, All rights reserved
////////////////////////////////////////////////////////////////////////////

package main

import (
	"fmt"

	"gopkg.in/suntong/enum.v1"
)

var (
	// Note this is a v1 example, i.e.,
	// the enum variables are declared,
	// instead of defined with `enum.NewEnum()`
	example enum.Enum
	Alpha   = example.Iota("Alpha")
	Beta    = example.Iota("Beta")

	weekday enum.Enum
	Sunday  = weekday.Iota("Sunday")
	Monday  = weekday.Iota("Monday")

	wkday enum.Enum
	Suday = wkday.Iota("Su")
	Moday = wkday.Iota("Mo")
	Tuday = wkday.Iota("Tu")
	Weday = wkday.Iota("We")
	Thday = wkday.Iota("Th")
	Frday = wkday.Iota("Fr")
	Saday = wkday.Iota("Sa")
)

// for standalone test, change package to main and the next func def to,
// func main() {
func main() {
	fmt.Printf("%s\n", example.String(Alpha))
	fmt.Printf("%s\n", example.String(Beta))
	fmt.Println("=======")
	fmt.Printf("%d\t%d\n", Alpha, Alpha+1)
	fmt.Printf("%s\t%s\n", example.String(Beta-1), example.String(Alpha+1))
	fmt.Println("=======")
	if a, ok := example.Get("Alpha"); ok {
		fmt.Printf("%d: %s\n", a, example.String(a))
	}
	if b, ok := example.Get("Beta"); ok {
		fmt.Printf("%d: %s\n", b, example.String(b))
	}

	fmt.Printf("%d:%s\n", Sunday, weekday.String(Sunday))
	fmt.Printf("%d:%s\n", Monday, weekday.String(Monday))
	fmt.Println("=======")

	fmt.Printf("%s: %d\n", wkday.String(Suday), Suday)
	fmt.Printf("%s: %d\n", wkday.String(Moday), Moday)
	fmt.Printf("%s: %d\n", wkday.String(Moday+Thday), Frday)
	fmt.Printf("%s: %d\n", wkday.String(Saday), Weday*2)
}
Output:

Alpha
Beta
=======
0	1
Alpha	Beta
=======
0: Alpha
1: Beta
0:Sunday
1:Monday
=======
Su: 0
Mo: 1
Fr: 5
Sa: 6

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Enum

type Enum struct {
	// contains filtered or unexported fields
}

Enum holds the enumerables

func NewEnum

func NewEnum() Enum

func (Enum) Exist

func (e Enum) Exist(v int) (string, bool)

Exist returns Enum's string form if exist

func (Enum) Get

func (e Enum) Get(s string) (int, bool)

Get lookup the given string for interal enumerable

func (*Enum) Iota

func (e *Enum) Iota(s string) int

Iota converts string to enumerable, similar to Go's iota

func (*Enum) IotaAt

func (e *Enum) IotaAt(s string, at int) int

IotaAt converts string to the enumerable given as the at value

func (Enum) String

func (e Enum) String(v int) string

String turns Enum to its string form

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL