Use Python package enum type in GoLang app
This article provides an introduction to cross-technology handling of enum
type. In programming, an enum
(short for enumeration) type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Enums are used to create our own data types, just like classes. They are particularly useful when we want to represent a fixed set of constants, such as days of the week, states, colors, directions, and more. This makes the code more readable and less prone to errors.
Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology. If have not yet created your first project check Javonet overview and quick start guides for your technology.
With Javonet, you can interact with enum
types from Python package as if they were available in GoLang, but interaction must be performed through the Javonet SDK API. This allows you to handle and manipulate enum
values, pass them to methods, and return them from methods, all while maintaining the type safety and readability of your GoLang code.
Custom Python package with enum type
With Javonet it is possible to reference any custom Python package and interact with its methods declared on types defined within that module almost the same as with any other GoLang library.
Snippet below represents the sample code from Python package which contains enum
type.
from enum import Enum
class Fruit(Enum):
Apple = 1
Banana = 2
Orange = 3
Mango = 4
Fruits = []
@staticmethod
def add_fruits_to_list(fruits):
TestClass.Fruits.extend(fruits)
return "{0} fruits on the list".format(len(TestClass.Fruits))
Javonet SDK contains various methods to interact with enums and consume the results in GoLang:
// use Activate only once in your app
Javonet.ActivateWithCredentials("your-license-key")
// create called runtime context
calledRuntime, _ := Javonet.InMemory().Python()
// set up variables
libraryPath := resourcesDirectory
className := "TestClass.TestClass"
// load custom library
calledRuntime.LoadLibrary(libraryPath)
// get type from the runtime
calledRuntimeType, _ := calledRuntime.GetType(className).Execute()
//create enum items
fruit1 := calledRuntime.GetEnumItem(calledRuntimeType, "Fruit", "Mango")
fruit2 := calledRuntime.GetEnumItem(calledRuntimeType, "Fruit", "Orange")
//get items' names and values
response1, _ := fruit1.GetEnumName().Execute()
fruit1Name := response1.GetValue().(string)
response2, _ := fruit2.GetEnumName().Execute()
fruit2Name := response2.GetValue().(string)
response3, _ := fruit1.GetEnumValue().Execute()
fruit1Value := response3.GetValue().(int32)
response4, _ := fruit2.GetEnumValue().Execute()
fruit2Value := response4.GetValue().(int32)
// write result to console
result := fmt.Sprintf("%v: %d, %v: %d", fruit1Name, fruit1Value, fruit2Name, fruit2Value)
fmt.Println(result)
This snippet uses in memory runtime bridging to load the Python package and next retrieves reference to specific enum
type. Then two enum
items are created. Their values and names are checked.
Enum items can be passed to Python package method:
In this example, two enum
items are created, too. Then array of enums is created and passed to method which adds enum
items to list.
The same operation can be performed remotely by just changing the new Runtime Context invocation from in memory to tcp that will create and interact with your Python package objects on any remote node, container or service that hosts Javonet Code Gateway. This way you can preserve the same logic in your application and instantly switch between monolithic and microservices architecture without the need to implement the integration layer based on web services or other remote invocation methods.
Read more about use cases and software architecture scenarios where Javonet runtime bridging technology can support your development process.
Was this article helpful?