Skip to content

Commit

Permalink
Move DefaultURI to openapi package
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitin Mohan committed May 31, 2019
1 parent bbb1a5d commit e3ddfc2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 60 deletions.
27 changes: 0 additions & 27 deletions expr/server.go
Expand Up @@ -236,33 +236,6 @@ func (h *HostExpr) HasGRPCScheme() bool {
return false
}

// DefaultURI returns the first URI defined in the host. It substitutes any URI
// parameters with their default values or the first item in their enum. This
// method must be used only after the finalize phase.
func (h *HostExpr) DefaultURI() string {
if len(h.URIs) == 0 {
return ""
}
u := h.URIs[0]
ustr := string(u)
vars := AsObject(h.Variables.Type)
if len(*vars) == 0 {
return ustr
}
for _, p := range u.Params() {
for _, v := range *vars {
if p == v.Name {
def := v.Attribute.DefaultValue
if def == nil {
def = v.Attribute.Validation.Values[0]
}
ustr = strings.Replace(ustr, fmt.Sprintf("{%s}", p), fmt.Sprintf("%v", def), -1)
}
}
}
return ustr
}

// Params return the names of the parameters used in URI if any.
func (u URIExpr) Params() []string {
r := regexp.MustCompile(`\{([^\{\}]+)\}`)
Expand Down
32 changes: 0 additions & 32 deletions expr/server_test.go
Expand Up @@ -318,35 +318,3 @@ func TestHostExprAttribute(t *testing.T) {
}
}
}

func TestHostExprDefaultURI(t *testing.T) {
cases := map[string]struct {
hostExpr *HostExpr
expected string
}{
"no-variables": {
hostExpr: &HostExpr{Name: "default", URIs: []URIExpr{URIExpr("https://goa.design"), URIExpr("grpcs://goa.design")}},
expected: "https://goa.design",
},
"with-variables": {
hostExpr: &HostExpr{
Name: "default",
URIs: []URIExpr{URIExpr("https://{version}.goa.design.{domain}"), URIExpr("grpcs://{version}.goa.design")},
Variables: &AttributeExpr{Type: &Object{
&NamedAttributeExpr{Name: "version", Attribute: &AttributeExpr{Type: String, DefaultValue: "v1"}},
&NamedAttributeExpr{Name: "domain", Attribute: &AttributeExpr{Type: String, Validation: &ValidationExpr{Values: []interface{}{"org", "com"}}}},
}},
},
expected: "https://v1.goa.design.org",
},
}
for k, tc := range cases {
t.Run(k, func(t *testing.T) {
tc.hostExpr.Finalize()
actual := tc.hostExpr.DefaultURI()
if actual != tc.expected {
t.Errorf("got %#v, expected %#v", actual, tc.expected)
}
})
}
}
28 changes: 27 additions & 1 deletion http/codegen/openapi/openapi_v2_builder.go
Expand Up @@ -19,7 +19,7 @@ func NewV2(root *expr.RootExpr, h *expr.HostExpr) (*V2, error) {
return nil, nil
}
tags := tagsFromExpr(root.Meta)
u, err := url.Parse(h.DefaultURI())
u, err := url.Parse(defaultURI(h))
if err != nil {
// This should never happen because server expression must have been
// validated. If it does, then we must fix server validation.
Expand Down Expand Up @@ -132,6 +132,32 @@ func ExtensionsFromExpr(mdata expr.MetaExpr) map[string]interface{} {
return extensions
}

// defaultURI returns the first URI defined in the host. It substitutes any URI
// parameters with their default values or the first item in their enum.
func defaultURI(h *expr.HostExpr) string {
if len(h.URIs) == 0 {
return ""
}
u := h.URIs[0]
ustr := string(u)
vars := expr.AsObject(h.Variables.Type)
if len(*vars) == 0 {
return ustr
}
for _, p := range u.Params() {
for _, v := range *vars {
if p == v.Name {
def := v.Attribute.DefaultValue
if def == nil {
def = v.Attribute.Validation.Values[0]
}
ustr = strings.Replace(ustr, fmt.Sprintf("{%s}", p), fmt.Sprintf("%v", def), -1)
}
}
}
return ustr
}

// mustGenerate returns true if the meta indicates that a OpenAPI specification should be
// generated, false otherwise.
func mustGenerate(meta expr.MetaExpr) bool {
Expand Down

0 comments on commit e3ddfc2

Please sign in to comment.