New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Service() #1139
Implement Service() #1139
Conversation
806bcf5
to
13ac7d5
Compare
13ac7d5
to
c6a1636
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Left a few small comments.
One thing that might be missing is the case where a payload or result type uses another type in the design. In this case that other type also needs to be generated. Seems like the code needs to traverse all the attributes recursively and collect all the types (using Walk https://github.com/goadesign/goa/blob/v2/design/types.go#L68).
codegen/files/service.go
Outdated
{ | ||
header = codegen.Header(s.service.Name+"Services", "services", | ||
[]*codegen.ImportSpec{ | ||
&codegen.ImportSpec{Path: "context"}, |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
codegen/files/service.go
Outdated
methods := make([]*serviceMethod, len(s.service.Endpoints)) | ||
for i, v := range s.service.Endpoints { | ||
fields := make(map[string]string) | ||
if o, ok := v.Payload.Attribute().Type.(design.Object); ok { |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
codegen/files/service.go
Outdated
const serviceT = `type ( | ||
// {{ .VarName }} is the {{ .Name }} service interface. | ||
{{ .VarName }} interface { | ||
{{ range .Methods }} // {{ .Name }} implements the {{ .Name }} endpoint. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
codegen/files/service_test.go
Outdated
&b, | ||
}, | ||
} | ||
_ = multipleEndpoints |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Thanks for all the changes, looks good. Are you working on recursively tracking the types that need to be generated? seems like it's the last item remaining. |
Yes, I'm working on that. It seems we have to improve |
6587e5c
to
ce94d24
Compare
ce94d24
to
abddedc
Compare
The |
It's a big topic for |
I pushed a new commit.
|
In the way you shown, I could track only top level fields (means We also might need a function like |
Yes you're right - it has to be recursive. And also agree on needing something like |
Looks good overall! a couple of comments: Cycles I think the walker code needs to handle cycles, consider this design: var Parent = Type("parent", func() {
Attribute("c", Child)
})
var Child = Type("child", func() {
Attribute("p", "parent")
}) This is a legal definition that will cause the walker to recurse infinitely. I think it needs to recurse only if the type is not already in the hash. Indenting Yes the code gets So @michaelboke @tchssk let's do a quick vote here :) do we indent the generated code using |
|
07a5805
to
ea09cbe
Compare
ea09cbe avoids infinite recursion in |
Gofmt but keep formatting as good as possible in templates. At least I'm
trying
Op do 30 mrt. 2017 om 18:09 schreef Taichi Sasaki <notifications@github.com>
… Cycles
ea09cbe
<ea09cbe>
avoids infinite recursion in Service().
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1139 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ACBg1F4dyf-OTqPCpqIU9OVenrlVoWmFks5rq9PJgaJpZM4Mm1jw>
.
|
Agree on keeping formatting as good as possible. The idea here is mainly to remove the tracking of indentation - that is remove the |
Once it seems finished. or Do I have anything to do more? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! thank you. It might be good to have a few more tests that cover all the case, something like:
- With empty payload, empty result
- With non empty payload but empty result
- With empty payload and non empty result
- With non empty payload and non empty result.
For the non empty tests it would also be good to cover all the data types (primitive, array, map, object, user and media types).
The tests here are good, I'm hoping we can be systematic and use variable names that are consistent for the others as well (like you have with APayload
etc.).
codegen/types.go
Outdated
return fmt.Sprintf("map[%s]%s", keyDef, elemDef) | ||
case design.Object: | ||
return goTypeDefObject(actual) | ||
case *design.UserTypeExpr: |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
codegen/types.go
Outdated
return fmt.Sprintf("map[%s]%s", GoNativeType(actual.KeyType.Type), GoNativeType(actual.ElemType.Type)) | ||
case design.Object: | ||
return "map[string]interface{}" | ||
case *design.UserTypeExpr: |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
codegen/types.go
Outdated
return fmt.Sprintf("map[%s]%s", GoNativeType(actual.KeyType.Type), GoNativeType(actual.ElemType.Type)) | ||
case design.Object: | ||
return "map[string]interface{}" | ||
case *design.UserTypeExpr: |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
codegen/types.go
Outdated
@@ -143,7 +143,7 @@ func goTypeDefObject(o design.Object) string { | |||
var ss []string | |||
ss = append(ss, "struct {") | |||
o.WalkAttributes(func(name string, at *design.AttributeExpr) error { | |||
ss = append(ss, fmt.Sprintf("%s %s", name, GoTypeName(at.Type))) | |||
ss = append(ss, fmt.Sprintf("\t%s %s", name, GoTypeName(at.Type))) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
a97bc6b uses |
codegen/types.go
Outdated
case *design.UserTypeExpr: | ||
return GoTypeName(actual) | ||
case design.UserType: | ||
return GoTypeName(actual.Attribute().Type) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
fc7a5b1 adds tests about empty payload and empty result. |
Travis failed with error that |
Yes this PR moved the walkers to the code generation package ( |
Shoud I rebase this branch onto v2? |
Yeah, that would probably make sense |
Actually I'll merge and rebase since I need some of the changes in this PR (the |
* Implement Service() * Use design.AsObject() instead of type assertion * Elide the respective literal type * Delete debug codes * Use Name and VarName differently in serviceMethod * Add tests for GoNativeType() * Support Map, Object and UserType in GoNativeType() * Track the types recursively in Service() * Separate template using define * Implement GoTypeDef() * Render results in Service() * Track the types recursively in Service() * Avoid infinite recursion in Service() * Remove indents and use gofmt for tests * Set indents in goTypeDefObject() * Set indents in Service() * Use UserType instead of UserTypeExpr * Fix GoTypeDef() about UserType * Add tests about empty payload and empty result
#1106