Beispiel #1
0
func addSwaggerResponse(responses map[string]*SwaggerResponse, errType string, sym string, errComment string) {
	code := rdl.StatusCode(sym)
	var schema *SwaggerType
	if sym != "NO_CONTENT" {
		schema = new(SwaggerType)
		schema.Ref = "#/definitions/" + errType
	}
	description := rdl.StatusMessage(sym)
	if errComment != "" {
		description += " - " + errComment
	}
	responses[code] = &SwaggerResponse{description, schema}
}
Beispiel #2
0
func formatResource(out io.Writer, registry rdl.TypeRegistry, rez *rdl.Resource) {
	fmt.Fprintf(out, "\n#### %s %s\n", strings.ToUpper(rez.Method), rez.Path)
	if rez.Comment != "" {
		fmt.Fprintf(out, "%s", formatBlock(rez.Comment, 0, 80, ""))
	}
	if len(rez.Inputs) > 0 {
		var rows [][]string
		for _, f := range rez.Inputs {
			fn := string(f.Name)
			ft := annotate(registry, f.Type)
			fs := ""
			if f.PathParam {
				fs = "path"
			} else if f.QueryParam != "" {
				fs = "query: " + f.QueryParam
			} else if f.Header != "" {
				fs = "header: " + f.Header
				//			} else if f.Context != "" {
				//				fs = "context: " + f.Context
			} else {
				fs = "body"
			}
			fo := ""
			if f.Optional {
				fo = "optional"
			}
			if f.Default != nil {
				s := optionalAnyToString(f.Default)
				if fo == "" {
					fo = "default=" + s
				} else {
					fo += ", default=" + s
				}
			}
			if f.Pattern != "" {
				if fo != "" {
					fo += ", "
				}
				fo += "pattern: " + f.Pattern
			}
			if f.Flag {
				if fo != "" {
					fo += ", "
				}
				fo += "flag"
			}
			fc := ""
			if f.Comment != "" {
				fc += f.Comment
			}
			row := []string{fn, ft, fs, fo, fc}
			rows = append(rows, row)
		}
		if rows != nil {
			fmt.Fprintf(out, "\n#### Request parameters:\n\n")
			formatTable(out, []string{"Name", "Type", "Source", "Options", "Description"}, rows)
		}
	}
	if len(rez.Outputs) > 0 {
		var rows [][]string
		for _, f := range rez.Outputs {
			fn := string(f.Name)
			ft := annotate(registry, f.Type)
			fd := "header: " + f.Header
			fo := "false"
			if f.Optional {
				fo = "true"
			}
			fc := ""
			if f.Comment != "" {
				fc = f.Comment
			}
			row := []string{fn, ft, fd, fo, fc}
			rows = append(rows, row)
		}
		if rows != nil {
			fmt.Fprintf(out, "\n#### Response parameters:\n\n")
			formatTable(out, []string{"Name", "Type", "Destination", "Optional", "Description"}, rows)
		}
	}
	fmt.Fprintf(out, "\n#### Responses:\n\n")
	var results [][]string
	if rez.Expected != "OK" {
		e := rez.Expected
		s := ""
		if e != "NO_CONTENT" {
			s = annotate(registry, rez.Type)
		}
		results = append(results, []string{rdl.StatusCode(e) + " " + rdl.StatusMessage(e), s})
	} else {
		results = append(results, []string{"200 " + rdl.StatusMessage("OK"), string(rez.Type)})
	}
	if len(rez.Alternatives) > 0 {
		for _, v := range rez.Alternatives {
			s := ""
			if v != "NO_CONTENT" {
				s = annotate(registry, rez.Type)
			}
			results = append(results, []string{rdl.StatusCode(v) + " " + rdl.StatusMessage(v), s})
		}
	}
	fmt.Fprintf(out, "Expected:\n\n")
	formatTable(out, []string{"Code", "Type"}, results)

	if len(rez.Exceptions) > 0 {
		var rows [][]string
		for ec, edef := range rez.Exceptions {
			etype := edef.Type
			et := annotate(registry, rdl.TypeRef(etype))
			ecomment := edef.Comment
			row := []string{rdl.StatusCode(ec) + " " + rdl.StatusMessage(ec), et, ecomment}
			rows = append(rows, row)
		}
		if rows != nil {
			sort.Sort(byCode(rows))
			fmt.Fprintf(out, "\nException:\n\n")
			formatTable(out, []string{"Code", "Type", "Comment"}, rows)
		}
	}
}