Skip to content

Commit

Permalink
Add support for simplified version of Enum for typescriptify
Browse files Browse the repository at this point in the history
  • Loading branch information
APshenkin committed Nov 10, 2023
1 parent 9cb76ac commit 14301ef
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions v2/internal/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,27 @@ func (b *Bindings) WriteModels(modelsDir string) error {
func (b *Bindings) AddEnumToGenerateTS(e interface{}) {
enumType := reflect.TypeOf(e)

var packageName string
var enumName string
// enums should be represented as array of all possible values
if hasElements(enumType) {
packageName := getPackageName(enumType.Elem().String())
enumName := enumType.Elem().Name()
enum := enumType.Elem()
println(enum.Kind())
// simple enum represented by struct with Value/TSName fields
if enum.Kind() == reflect.Struct {
_, tsNamePresented := enum.FieldByName("TSName")
enumT, valuePresented := enum.FieldByName("Value")
if tsNamePresented && valuePresented {
packageName = getPackageName(enumT.Type.String())
enumName = enumT.Type.Name()
} else {
return
}
// otherwise expecting implementation with TSName() https://github.com/tkrajina/typescriptify-golang-structs#enums-with-tsname
} else {
packageName = getPackageName(enumType.Elem().String())
enumName = enumType.Elem().Name()
}
if b.enumsToGenerateTS[packageName] == nil {
b.enumsToGenerateTS[packageName] = make(map[string]interface{})
}
Expand Down

0 comments on commit 14301ef

Please sign in to comment.