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

added check associated variables #24

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
10 changes: 10 additions & 0 deletions pkg/processor/httpmethod.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

const (
errHTTPMethodGETCouldNotHaveRequestBody = "http method GET could not have request body in %s interface %s method %s"
errHTTPMethodCouldNoAssociatedVariable = "http method no associated variable with %s variable %s, available variables %s"
)

// HTTPMethod ...
Expand Down Expand Up @@ -51,6 +52,9 @@ func (s *httpMethod) Process(httpMethod *api.HTTPMethod, iface *api.Interface, m
diff[arg.Name] = arg.Type.String()
}
for from, toPlaceholder = range httpMethod.QueryPlaceholders {
if _, ok := diff[toPlaceholder.Name]; !ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

давай рассмотрим другой вариант, предлагаю что то типа такого:

type ErrorProcessor struct {
    template string
}

func (e *ErrorProcessor) Process(data map[string]string, key string, tag string) {
    if _, ok := data[key]; !ok {
			return fmt.Errorf(e.template, tag, key, e.keys2String(data))
		}
}

func (e *ErrorProcessor) keys2String(v map[string]string) string {
	keys := make([]string, 0, len(v))
	for key := range v {
		keys = append(keys, key)
	}
	return strings.Join(keys, ", ")
}

и этот процессор отдаешь в место использования

return fmt.Errorf(errHTTPMethodCouldNoAssociatedVariable, "query", toPlaceholder.Name, keys2String(diff))
}
delete(diff, toPlaceholder.Name)
for _, arg = range args {
if arg.Name == toPlaceholder.Name {
Expand All @@ -59,9 +63,15 @@ func (s *httpMethod) Process(httpMethod *api.HTTPMethod, iface *api.Interface, m
}
}
for _, to = range httpMethod.HeaderPlaceholders {
if _, ok := diff[to]; !ok {
return fmt.Errorf(errHTTPMethodCouldNoAssociatedVariable, "header", to, keys2String(diff))
}
delete(diff, to)
}
for _, to = range httpMethod.URIPathPlaceholders {
if _, ok := diff[to]; !ok {
return fmt.Errorf(errHTTPMethodCouldNoAssociatedVariable, "URI path", to, keys2String(diff))
}
delete(diff, to)
}
if httpMethod.Method == http.MethodGet && len(diff) > 0 {
Expand Down
11 changes: 11 additions & 0 deletions pkg/processor/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package processor

import "strings"

func keys2String(v map[string]string) string {
keys := make([]string, 0, len(v))
for key := range v {
keys = append(keys, key)
}
return strings.Join(keys, ", ")
}