Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(goctl): better generate the api code of typescript (#2483)
- Loading branch information
Showing
4 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |