🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

github.com/YabZhang/zstringer

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/YabZhang/zstringer

v0.0.0-20230330071402-d28222cda325
Source
Go
Version published
Created
Source

说明

golang.org/x/tools/cmd/stringer 源码做修改,用法相同。

新增 go generate 生成代码包含解析方法 func Parse%{typeName}String(s string) (%{typeName}s, bool)

Example

// event.go
package event

// 定义事件
//
//go:generate zstringer -type=Event
type Event int

const (
	InsertCoinEvent Event = iota
	SelectItemEvent
	ItemConfirmEvent
)

生成代码

// Code generated by "zstringer -type=Event"; DO NOT EDIT.

package event

import "strconv"

func _() {
	// An "invalid array index" compiler error signifies that the constant values have changed.
	// Re-run the zstringer command to generate them again.
	var x [1]struct{}
	_ = x[InsertCoinEvent-0]
	_ = x[SelectItemEvent-1]
	_ = x[ItemConfirmEvent-2]
}

const _Event_name = "InsertCoinEventSelectItemEventItemConfirmEvent"

var _Event_index = [...]uint8{0, 15, 30, 46}
var _Event_inst = [...]Event{InsertCoinEvent, SelectItemEvent, ItemConfirmEvent}

func (i Event) String() string {
	if i < 0 || i >= Event(len(_Event_index)-1) {
		return "Event(" + strconv.FormatInt(int64(i), 10) + ")"
	}
	return _Event_name[_Event_index[i]:_Event_index[i+1]]
}

func ParseEventString(s string) (Event, bool) {
	if len(s) == 0 || len(_Event_inst) == 0 {
		return 0, false
	}
	for _, inst := range _Event_inst {
		if inst.String() == s {
			return inst, true
		}
	}
	return 0, false
}

FAQs

Package last updated on 30 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts