Example #1
0
func opConsumesSetter(op *spec.Operation) func([]string) {
	return func(consumes []string) { op.Consumes = consumes }
}
Example #2
0
func buildOperation(route *revel.Route) *spec.Operation {
	var (
		typeInfo   *harness.TypeInfo
		methodSpec *harness.MethodSpec
	)

	info, rerr := harness.ProcessSource(revel.CodePaths)
	if rerr != nil {
		panic(rerr) // TODO EMPTY PANIC
	}

	// get the TypeInfo and MethodSpec for this route
	for _, cinfo := range info.ControllerSpecs() {
		typeInfo = cinfo // TODO move inside if (get around compiler complaint)
		if route.ControllerName == typeInfo.StructName {
			for _, spec := range cinfo.MethodSpecs {
				if route.MethodName == spec.Name {
					methodSpec = spec
					break
				}
			}
			break
		}
	}

	op := new(spec.Operation)
	// TODO op.Description
	// this will probably require either editing harness.ProcessSource to also grab comments OR
	// to copy that functionality and modify it
	op.Consumes = ContentTypes
	op.Produces = ContentTypes
	op.AddExtension("x-revel-action", route.Action)

	for i, arg := range methodSpec.Args {
		// skip over fixed paramters that match up to the arguments
		if i < len(route.FixedParams) {
			continue
		}
		var param spec.Parameter
		param.Name = arg.Name
		param.Type = arg.TypeExpr.Expr

		// TODO review
		// TODO: better path vs query vs body vs multipart
		count := strings.Count(route.Path, ":") + strings.Count(route.Path, "*")
		if i < count {
			param.In = "path"
		} else {
			param.In = "body"
		}
		op.Parameters = append(op.Parameters, param)
	}

	// TODO RenderCalls
	// fmt.Printf("route:       %#v\n", route)
	// fmt.Printf("typeInfo:    %#v\n", typeInfo)
	// fmt.Printf("methodSpec: %#v\n", methodSpec)
	// for _, call := range methodSpec.RenderCalls {
	// 	fmt.Printf("\tcall: %#v\n", call)
	// }

	/*
		"responses": {
		  "200": {
		    "description": "A list of pets.",
		    "schema": {
		      "type": "array",
		      "items": {
		        "$ref": "#/definitions/Pet"
		      }
		    }
		  }
		}
	*/

	return op
}