feat(goctl): better generate the api code of typescript (#2483)
parent
74cc6b55e8
commit
799c118d95
@ -0,0 +1,15 @@
|
||||
package spec_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
||||
)
|
||||
|
||||
func ExampleMember_GetEnumOptions() {
|
||||
member := spec.Member{
|
||||
Tag: `json:"foo,options=foo|bar|options|123"`,
|
||||
}
|
||||
fmt.Println(member.GetEnumOptions())
|
||||
// Output:
|
||||
// [foo bar options 123]
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package tsgen
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenTsType(t *testing.T) {
|
||||
member := spec.Member{
|
||||
Name: "foo",
|
||||
Type: spec.PrimitiveType{RawName: "string"},
|
||||
Tag: `json:"foo,options=foo|bar|options|123"`,
|
||||
Comment: "",
|
||||
Docs: nil,
|
||||
IsInline: false,
|
||||
}
|
||||
ty, err := genTsType(member)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, `'foo' | 'bar' | 'options' | '123'`, ty)
|
||||
|
||||
member.IsInline = true
|
||||
ty, err = genTsType(member)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, `'foo' | 'bar' | 'options' | '123'`, ty)
|
||||
|
||||
member.Type = spec.PrimitiveType{RawName: "int"}
|
||||
member.Tag = `json:"foo,options=1|3|4|123"`
|
||||
ty, err = genTsType(member)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, `1 | 3 | 4 | 123`, ty)
|
||||
}
|
Loading…
Reference in New Issue