tkrajina / typescriptify-golang-structs

A Golang struct to TypeScript class/interface converter
Apache License 2.0
505 stars 87 forks source link

check enum cast to fmt.Stringer interface for getting name of enum value #42

Closed asmyasnikov closed 3 years ago

asmyasnikov commented 3 years ago

PR contains:

tkrajina commented 3 years ago

Fine for me. Could you also add a test?

asmyasnikov commented 3 years ago

Ready)

tkrajina commented 3 years ago

Hm, I disagree with this one. Stringer is more for defining descriptive strings, TSName() isn't for descriptive strings, it's specifically to get typescript enum keys. If somebody wants to use the .String() (s)he can easily just

func (x ...) TSName() { return x.String() }

...without any change in the library.

asmyasnikov commented 3 years ago

Hm, I disagree with your disagreement Your idea cannot possible if need to typescriptify external struct, which not belong developer. But external structs can .String() often

tkrajina commented 3 years ago

Well, relying on a String() method from an external library sounds even worse to me.

Anyway, note that you don't have to use .TSName(), you can also define the struct names with:

var AllWeekdays = []struct {
    Value  Weekday
    TSName string
}{
    {Sunday, "SUNDAY"},
    {Monday, "MONDAY"},
    {Tuesday, "TUESDAY"},
    {Wednesday, "WEDNESDAY"},
    {Thursday, "THURSDAY"},
    {Friday, "FRIDAY"},
    {Saturday, "SATURDAY"},
}
asmyasnikov commented 3 years ago

For typescript codegen need first go-codegen for multiple enums It cannot make usability typescriptify-golang-structs for typescriptify golang structs For example, I want to typescriptify package https://github.com/asmyasnikov/go-mavlink/tree/master/mavlink/dialects/ardupilotmega But it cannot possible because for example https://github.com/asmyasnikov/go-mavlink/blob/master/mavlink/dialects/ardupilotmega/enums.go contains 14000+ lines Or https://github.com/asmyasnikov/go-mavlink/blob/master/mavlink/dialects/ardupilotmega/classes.go contains 16000+ lines By hand write mappings? It is not comfortably As you wish...