Skip to content

Commit

Permalink
Merge pull request #9 from vmkteam/support-go-external-packages
Browse files Browse the repository at this point in the history
Support go external packages
  • Loading branch information
zhuharev authored Jul 15, 2021
2 parents 32d7f85 + dea40df commit 594298d
Show file tree
Hide file tree
Showing 5 changed files with 398 additions and 120 deletions.
2 changes: 2 additions & 0 deletions golang/rpcgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
func TestGenerateGoClient(t *testing.T) {
rpc := zenrpc.NewServer(zenrpc.Options{})
rpc.Register("catalogue", testdata.CatalogueService{})
rpc.Register("phonebook", testdata.PhoneBook{})
rpc.Register("arith", testdata.ArithService{})

cl := NewClient(rpc.SMD())

Expand Down
19 changes: 14 additions & 5 deletions golang/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ type Value struct {
ModelName string
}

// LocalModelName rename external packages model names: pkg.Model -> PkgModel
func (v Value) LocalModelName() string {
return localModelName(v.ModelName)
}

func localModelName(name string) string {
return strings.ReplaceAll(titleFirstLetter(name), ".", "")
}

// GoType convert Value jsType to golang type
func (v *Value) GoType() string {
if v == nil {
Expand All @@ -175,16 +184,16 @@ func (v *Value) GoType() string {

if v.Type == smd.Array {
if v.ModelName != "" {
return fmt.Sprintf("[]%s", v.ModelName)
return fmt.Sprintf("[]%s", v.LocalModelName())
}

return fmt.Sprintf("[]%s", simpleGoType(v.ArrayItemType))
} else if v.Type == smd.Object {
if v.Optional {
return fmt.Sprintf("*%s", v.ModelName)
return fmt.Sprintf("*%s", v.LocalModelName())
}

return v.ModelName
return v.LocalModelName()
}

return simpleGoType(v.Type)
Expand Down Expand Up @@ -438,7 +447,7 @@ func newModels(in smd.JSONSchema, isParam, isReturn bool, modelNamePrefix string
}

model := Model{
Name: name,
Name: localModelName(name),
Fields: values,
IsParamModel: isParam,
IsReturnModel: isReturn,
Expand All @@ -458,7 +467,7 @@ func newModels(in smd.JSONSchema, isParam, isReturn bool, modelNamePrefix string
// Definition is always an object.
func convertDefinitionToModel(def smd.Definition, name string) Model {
model := Model{
Name: name,
Name: localModelName(name),
}

for _, property := range def.Properties {
Expand Down
Loading

0 comments on commit 594298d

Please sign in to comment.