Skip to content
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

Add sortable ServiceMethods for template use with requisite unit tests #534

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ type Processor interface {
// supplying a non-empty string as the last parameter.
//
// Example: generating an HTML template (assuming you've got a Template object)
// data, err := RenderTemplate(RenderTypeHTML, &template, "")
// data, err := RenderTemplate(RenderTypeHTML, &template, "")
//
// Example: generating a custom template (assuming you've got a Template object)
// data, err := RenderTemplate(RenderTypeHTML, &template, "{{range .Files}}{{.Name}}{{end}}")
// data, err := RenderTemplate(RenderTypeHTML, &template, "{{range .Files}}{{.Name}}{{end}}")
func RenderTemplate(kind RenderType, template *Template, inputTemplate string) ([]byte, error) {
if inputTemplate != "" {
processor := &textRenderer{inputTemplate}
Expand All @@ -113,6 +113,7 @@ type textRenderer struct {
}

func (mr *textRenderer) Apply(template *Template) ([]byte, error) {
funcMap["SortServiceMethods"] = SortServiceMethods
tmpl, err := text_template.New("Text Template").Funcs(funcMap).Funcs(sprig.TxtFuncMap()).Parse(mr.inputTemplate)
if err != nil {
return nil, err
Expand All @@ -131,6 +132,7 @@ type htmlRenderer struct {
}

func (mr *htmlRenderer) Apply(template *Template) ([]byte, error) {
funcMap["SortServiceMethods"] = SortServiceMethods
tmpl, err := html_template.New("Text Template").Funcs(funcMap).Funcs(sprig.HtmlFuncMap()).Parse(mr.inputTemplate)
if err != nil {
return nil, err
Expand Down
16 changes: 16 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Template struct {
Files []*File `json:"files"`
// Details about the scalar values and their respective types in supported languages.
Scalars []*ScalarValue `json:"scalarValueTypes"`
// Function Map usable within Templates
}

// NewTemplate creates a Template object from a set of descriptors.
Expand Down Expand Up @@ -534,6 +535,15 @@ func parseServiceMethod(pm *protokit.MethodDescriptor) *ServiceMethod {
}
}

func SortServiceMethods(methods []*ServiceMethod) orderedMethods {
sortedMethods := make(orderedMethods, 0, len(methods))
for _, method := range methods {
sortedMethods = append(sortedMethods, method)
}
sort.Sort(sortedMethods)
return sortedMethods
}

func baseName(name string) string {
parts := strings.Split(name, ".")
return parts[len(parts)-1]
Expand Down Expand Up @@ -597,3 +607,9 @@ type orderedServices []*Service
func (os orderedServices) Len() int { return len(os) }
func (os orderedServices) Swap(i, j int) { os[i], os[j] = os[j], os[i] }
func (os orderedServices) Less(i, j int) bool { return os[i].LongName < os[j].LongName }

type orderedMethods []*ServiceMethod

func (om orderedMethods) Len() int { return len(om) }
func (om orderedMethods) Swap(i, j int) { om[i], om[j] = om[j], om[i] }
func (om orderedMethods) Less(i, j int) bool { return om[i].Name < om[j].Name }
16 changes: 16 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,22 @@ func TestServiceMethodProperties(t *testing.T) {
require.True(t, *method.Option(E_ExtendMethod.Name).(*bool))
}

func TestServiceMethodSorting(t *testing.T) {
service := findService("VehicleService", vehicleFile)

// verify 'by file' order by default
unsortedMethods := service.Methods
require.Equal(t, "GetModels", unsortedMethods[0].Name)
require.Equal(t, "AddModels", unsortedMethods[1].Name)
require.Equal(t, "GetVehicle", unsortedMethods[2].Name)

// verify 'a-z' order when sorted
sortedMethods := SortServiceMethods(service.Methods)
require.Equal(t, "AddModels", sortedMethods[0].Name)
require.Equal(t, "GetModels", sortedMethods[1].Name)
require.Equal(t, "GetVehicle", sortedMethods[2].Name)
}

func TestExcludedComments(t *testing.T) {
message := findMessage("ExcludedMessage", vehicleFile)
require.Empty(t, message.Description)
Expand Down