Giter VIP home page Giter VIP logo

enum2go's Introduction

Export enums from C to Go.

For when you need a bunch of enums from a C header file in Go.

Status

It compiles. Ship it!

Usage

Give the header file, possible cpp include paths, and the names of the enums you want. The Go package will be written to STDOUT.

enum2go -h some/header.h -I /usr/include myenum anotherenum > gen_enums.go

Or bring your own cpp:

cpp -I /usr/include some/header.h | enum2go -cpp="" myenum anotherenum > gen_enums.go

enum2go can be called from go:generate:

//go:generate enum2go -package $GOPACKAGE -h ./include/some/header.h -I ./include/ -I /usr/include -o gen_enums.go myenum anotherenum

By default we run the header through cpp (which needs to be available in $PATH). -I (include directories) flags given to enum2go will be passed on to cpp.

Example generated code

Original C header file (stripped to fit in here):

/*
 * This program is free software &c.
 */

#include <sys/types.h>

// ...

/* conntrack attributes */
enum nf_conntrack_attr {
	ATTR_ORIG_IPV4_SRC = 0,			/* u32 bits */
	ATTR_IPV4_SRC = ATTR_ORIG_IPV4_SRC,	/* alias */
	ATTR_ORIG_IPV4_DST,			/* u32 bits */
	ATTR_IPV4_DST = ATTR_ORIG_IPV4_DST,	/* alias */
	ATTR_REPL_IPV4_SRC,			/* u32 bits */
	ATTR_REPL_IPV4_DST,			/* u32 bits */
	ATTR_ORIG_IPV6_SRC = 4,			/* u128 bits */
	ATTR_IPV6_SRC = ATTR_ORIG_IPV6_SRC,	/* alias */
    // ...
	ATTR_MAX
};
// ...

Generated Go code:

package main

// code generated by enum2go.

type NfConntrackAttr int
const (
	AttrOrigIpv4Src NfConntrackAttr = 0
	AttrIpv4Src NfConntrackAttr = 0
	AttrOrigIpv4Dst NfConntrackAttr = 1
	AttrIpv4Dst NfConntrackAttr = 1
	AttrReplIpv4Src NfConntrackAttr = 2
	AttrReplIpv4Dst NfConntrackAttr = 3
	AttrOrigIpv6Src NfConntrackAttr = 4
	AttrIpv6Src NfConntrackAttr = 4
	AttrMax NfConntrackAttr = 5
)

Caveats

Plenty of those.

  • The C code tokeniser is very simplistic, but it is good enough for the enums I need.
  • Enum values can be either empty (iota style), a simple integer, or refer to another value in the same enum. Anything else is currently not supported, although some cases would be possible to add (1<<3 comes to mind).
  • Only named enums are supported.

enum2go's People

Stargazers

 avatar  avatar  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.