Help us shape the future of Go! Take the 2026 Go Developer Survey

polyglot-converter

command module
v0.0.0-...-856a3d8 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2021 License: MIT Imports: 8 Imported by: 0

README

Polyglot Converter

Motivation:
In the example scenario where you have a frontend (TypeScript) and 2 backend services (Go, Python), you want to use the same enums throughout, for consistency, less errors and letting your IDE help you with autocomplete.

Example

Full example here

  • Input
Color:
  Red: 0
  • Output
// Go
type Color struct {}
func (a Color) Red() int { return 0 }
func (a Color) RedKey() string { return "Red" }
func (a Color) GetKey(value int) string {
    if value == a.Red() { return a.RedKey() }
    return ""
}
func (a Color) GetValue(key string) int {
    if key == a.RedKey() { return a.Red() }
    return -1
}
// TypeScript
class Color {
	static Red = 0
	static RedKey = "Red"
	static GetKey (value: number): string {
		if (value === this.Red) { return this.RedKey }
		return ""
	}
	static GetValue (key: string): number {
		if (key === this.RedKey) { return this.Red }
		return -1
	}
}
# Python
class Color:
	Red = 0
	RedKey = "Red"
	@staticmethod
	def GetKey(value: int) -> str:
		if value == Color.Red: return Color.RedKey
		return ""
	@staticmethod
	def GetValue(key: str) -> int:
		if key == Color.RedKey: return Color.Red
		return -1

Features

  • Enum String
  • Enum Number
  • Switch args to flags cobra
  • Package option for Go (default main)

Language support

Go
TypeScript
Python 3

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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