本文整理匯總了Golang中github.com/go-openapi/analysis.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestGenerateServer_Parameters
func TestGenerateServer_Parameters(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.discriminators.yml")
if assert.NoError(t, err) {
method, path, op, ok := analysis.New(specDoc.Spec()).OperationForName("modelOp")
if assert.True(t, ok) {
bldr := codeGenOpBuilder{
Name: "modelOp",
Method: method,
Path: path,
APIPackage: "restapi",
ModelsPackage: "models",
Principal: "",
Target: ".",
Doc: specDoc,
Analyzed: analysis.New(specDoc.Spec()),
Operation: *op,
Authed: false,
DefaultScheme: "http",
ExtraSchemas: make(map[string]GenSchema),
}
genOp, err := bldr.MakeOperation()
if assert.NoError(t, err) {
assert.True(t, genOp.Responses[200].Schema.IsBaseType)
var buf bytes.Buffer
err := parameterTemplate.Execute(&buf, genOp)
if assert.NoError(t, err) {
res := buf.String()
assertInCode(t, "Pet models.Pet", res)
assertInCode(t, "body, err := models.UnmarshalPet(r.Body, route.Consumer)", res)
assertInCode(t, "o.Pet = body", res)
}
}
}
}
}
示例2: opBuilder
func opBuilder(name, fname string) (codeGenOpBuilder, error) {
if fname == "" {
fname = "../fixtures/codegen/todolist.simple.yml"
}
specDoc, err := loads.Spec(fname)
if err != nil {
return codeGenOpBuilder{}, err
}
analyzed := analysis.New(specDoc.Spec())
method, path, op, ok := analyzed.OperationForName(name)
if !ok {
return codeGenOpBuilder{}, errors.New("No operation could be found for " + name)
}
return codeGenOpBuilder{
Name: name,
Method: method,
Path: path,
APIPackage: "restapi",
ModelsPackage: "models",
Principal: "models.User",
Target: ".",
Operation: *op,
Doc: specDoc,
Analyzed: analyzed,
Authed: false,
ExtraSchemas: make(map[string]GenSchema),
}, nil
}
示例3: Analyzed
// Analyzed creates a new analyzed spec document
func Analyzed(data json.RawMessage, version string) (*Document, error) {
if version == "" {
version = "2.0"
}
if version != "2.0" {
return nil, fmt.Errorf("spec version %q is not supported", version)
}
swspec := new(spec.Swagger)
if err := json.Unmarshal(data, swspec); err != nil {
return nil, err
}
origsqspec := new(spec.Swagger)
if err := json.Unmarshal(data, origsqspec); err != nil {
return nil, err
}
d := &Document{
Analyzer: analysis.New(swspec),
schema: swag20Schema,
spec: swspec,
raw: data,
origSpec: origsqspec,
}
return d, nil
}
示例4: NewRoutableContext
// NewRoutableContext creates a new context for a routable API
func NewRoutableContext(spec *loads.Document, routableAPI RoutableAPI, routes Router) *Context {
var an *analysis.Spec
if spec != nil {
an = analysis.New(spec.Spec())
}
ctx := &Context{spec: spec, api: routableAPI, analyzer: an}
return ctx
}
示例5: newDefaultRouteBuilder
func newDefaultRouteBuilder(spec *loads.Document, api RoutableAPI) *defaultRouteBuilder {
return &defaultRouteBuilder{
spec: spec,
analyzer: analysis.New(spec.Spec()),
api: api,
records: make(map[string][]denco.Record),
}
}
示例6: TestBuildDiscriminatorMap
func TestBuildDiscriminatorMap(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.discriminators.yml")
if assert.NoError(t, err) {
di := discriminatorInfo(analysis.New(specDoc.Spec()))
assert.Len(t, di.Discriminators, 1)
assert.Len(t, di.Discriminators["#/definitions/Pet"].Children, 2)
assert.Len(t, di.Discriminated, 2)
}
}
示例7: NewContext
// NewContext creates a new context wrapper
func NewContext(spec *loads.Document, api *untyped.API, routes Router) *Context {
var an *analysis.Spec
if spec != nil {
an = analysis.New(spec.Spec())
}
ctx := &Context{spec: spec, analyzer: an}
ctx.api = newRoutableUntypedAPI(spec, api, ctx)
return ctx
}
示例8: Expanded
// Expanded expands the ref fields in the spec document and returns a new spec document
func (d *Document) Expanded() (*Document, error) {
swspec := new(spec.Swagger)
if err := json.Unmarshal(d.raw, swspec); err != nil {
return nil, err
}
if err := spec.ExpandSpec(swspec); err != nil {
return nil, err
}
dd := &Document{
Analyzer: analysis.New(swspec),
spec: swspec,
schema: swag20Schema,
raw: d.raw,
origSpec: d.origSpec,
}
return dd, nil
}
示例9: TestEmptyOperationNames
func TestEmptyOperationNames(t *testing.T) {
doc, err := loads.Spec("../fixtures/codegen/todolist.simple.yml")
if assert.NoError(t, err) {
sp := doc.Spec()
sp.Paths.Paths["/tasks"].Post.ID = ""
sp.Paths.Paths["/tasks"].Post.AddExtension("origName", "createTask")
sp.Paths.Paths["/tasks/{id}"].Put.ID = ""
sp.Paths.Paths["/tasks/{id}"].Put.AddExtension("origName", "updateTask")
analyzed := analysis.New(sp)
ops := gatherOperations(analyzed, nil)
assert.Len(t, ops, 4)
_, exists := ops["PostTasks"]
assert.True(t, exists)
_, exists = ops["PutTasksID"]
assert.True(t, exists)
}
}
示例10: testAppGenertor
func testAppGenertor(t testing.TB, specPath, name string) (*appGenerator, error) {
specDoc, err := loads.Spec(specPath)
if !assert.NoError(t, err) {
return nil, err
}
analyzed := analysis.New(specDoc.Spec())
models, err := gatherModels(specDoc, nil)
if !assert.NoError(t, err) {
return nil, err
}
operations := gatherOperations(analyzed, nil)
if len(operations) == 0 {
return nil, errors.New("no operations were selected")
}
opts := testGenOpts()
apiPackage := mangleName(swag.ToFileName(opts.APIPackage), "api")
return &appGenerator{
Name: appNameOrDefault(specDoc, name, "swagger"),
Receiver: "o",
SpecDoc: specDoc,
Analyzed: analyzed,
Models: models,
Operations: operations,
Target: ".",
DumpData: opts.DumpData,
Package: apiPackage,
APIPackage: apiPackage,
ModelsPackage: mangleName(swag.ToFileName(opts.ModelPackage), "definitions"),
ServerPackage: mangleName(swag.ToFileName(opts.ServerPackage), "server"),
ClientPackage: mangleName(swag.ToFileName(opts.ClientPackage), "client"),
Principal: opts.Principal,
DefaultScheme: "http",
DefaultProduces: runtime.JSONMime,
DefaultConsumes: runtime.JSONMime,
GenOpts: &opts,
}, nil
}
示例11: NewAPI
// NewAPI creates the default untyped API
func NewAPI(spec *loads.Document) *API {
var an *analysis.Spec
if spec != nil && spec.Spec() != nil {
an = analysis.New(spec.Spec())
}
return &API{
spec: spec,
analyzer: an,
DefaultProduces: runtime.JSONMime,
DefaultConsumes: runtime.JSONMime,
consumers: map[string]runtime.Consumer{
runtime.JSONMime: runtime.JSONConsumer(),
},
producers: map[string]runtime.Producer{
runtime.JSONMime: runtime.JSONProducer(),
},
authenticators: make(map[string]runtime.Authenticator),
operations: make(map[string]map[string]runtime.OperationHandler),
ServeError: errors.ServeError,
Models: make(map[string]func() interface{}),
formats: strfmt.NewFormats(),
}
}
示例12: newAppGenerator
func newAppGenerator(name string, modelNames, operationIDs []string, opts *GenOpts) (*appGenerator, error) {
if opts.TemplateDir != "" {
if err := templates.LoadDir(opts.TemplateDir); err != nil {
return nil, err
}
}
compileTemplates()
// Load the spec
_, specDoc, err := loadSpec(opts.Spec)
if err != nil {
return nil, err
}
analyzed := analysis.New(specDoc.Spec())
models, err := gatherModels(specDoc, modelNames)
if err != nil {
return nil, err
}
operations := gatherOperations(analyzed, operationIDs)
if len(operations) == 0 {
return nil, errors.New("no operations were selected")
}
defaultScheme := opts.DefaultScheme
if defaultScheme == "" {
defaultScheme = "http"
}
defaultProduces := opts.DefaultProduces
if defaultProduces == "" {
defaultProduces = runtime.JSONMime
}
defaultConsumes := opts.DefaultConsumes
if defaultConsumes == "" {
defaultConsumes = runtime.JSONMime
}
apiPackage := mangleName(swag.ToFileName(opts.APIPackage), "api")
return &appGenerator{
Name: appNameOrDefault(specDoc, name, "swagger"),
Receiver: "o",
SpecDoc: specDoc,
Analyzed: analyzed,
Models: models,
Operations: operations,
Target: opts.Target,
// Package: filepath.Base(opts.Target),
DumpData: opts.DumpData,
Package: apiPackage,
APIPackage: apiPackage,
ModelsPackage: mangleName(swag.ToFileName(opts.ModelPackage), "definitions"),
ServerPackage: mangleName(swag.ToFileName(opts.ServerPackage), "server"),
ClientPackage: mangleName(swag.ToFileName(opts.ClientPackage), "client"),
Principal: opts.Principal,
DefaultScheme: defaultScheme,
DefaultProduces: defaultProduces,
DefaultConsumes: defaultConsumes,
GenOpts: opts,
}, nil
}
示例13: GenerateServerOperation
// GenerateServerOperation generates a parameter model, parameter validator, http handler implementations for a given operation
// It also generates an operation handler interface that uses the parameter model for handling a valid request.
// Allows for specifying a list of tags to include only certain tags for the generation
func GenerateServerOperation(operationNames, tags []string, includeHandler, includeParameters, includeResponses bool, opts GenOpts) error {
if opts.TemplateDir != "" {
if err := templates.LoadDir(opts.TemplateDir); err != nil {
return err
}
}
compileTemplates()
// Load the spec
_, specDoc, err := loadSpec(opts.Spec)
if err != nil {
return err
}
analyzed := analysis.New(specDoc.Spec())
ops := gatherOperations(analyzed, operationNames)
for operationName, opRef := range ops {
method, path, operation := opRef.Method, opRef.Path, opRef.Op
defaultScheme := opts.DefaultScheme
if defaultScheme == "" {
defaultScheme = sHTTP
}
defaultProduces := opts.DefaultProduces
if defaultProduces == "" {
defaultProduces = runtime.JSONMime
}
defaultConsumes := opts.DefaultConsumes
if defaultConsumes == "" {
defaultConsumes = runtime.JSONMime
}
apiPackage := mangleName(swag.ToFileName(opts.APIPackage), "api")
serverPackage := mangleName(swag.ToFileName(opts.ServerPackage), "server")
generator := operationGenerator{
Name: operationName,
Method: method,
Path: path,
APIPackage: apiPackage,
ModelsPackage: mangleName(swag.ToFileName(opts.ModelPackage), "definitions"),
ClientPackage: mangleName(swag.ToFileName(opts.ClientPackage), "client"),
ServerPackage: serverPackage,
Operation: *operation,
SecurityRequirements: analyzed.SecurityRequirementsFor(operation),
Principal: opts.Principal,
Target: filepath.Join(opts.Target, serverPackage),
Base: opts.Target,
Tags: tags,
IncludeHandler: includeHandler,
IncludeParameters: includeParameters,
IncludeResponses: includeResponses,
DumpData: opts.DumpData,
DefaultScheme: defaultScheme,
DefaultProduces: defaultProduces,
DefaultConsumes: defaultConsumes,
Doc: specDoc,
Analyzed: analyzed,
}
if err := generator.Generate(); err != nil {
return err
}
}
return nil
}
示例14: makeGenDefinitionHierarchy
func makeGenDefinitionHierarchy(name, pkg, container string, schema spec.Schema, specDoc *loads.Document, includeValidator, includeModel bool) (*GenDefinition, error) {
receiver := "m"
resolver := newTypeResolver("", specDoc)
resolver.ModelName = name
analyzed := analysis.New(specDoc.Spec())
di := discriminatorInfo(analyzed)
pg := schemaGenContext{
Path: "",
Name: name,
Receiver: receiver,
IndexVar: "i",
ValueExpr: receiver,
Schema: schema,
Required: false,
TypeResolver: resolver,
Named: true,
ExtraSchemas: make(map[string]GenSchema),
Discrimination: di,
Container: container,
IncludeValidator: includeValidator,
IncludeModel: includeModel,
}
if err := pg.makeGenSchema(); err != nil {
return nil, err
}
dsi, ok := di.Discriminators["#/definitions/"+name]
if ok {
// when these 2 are true then the schema will render as an interface
pg.GenSchema.IsBaseType = true
pg.GenSchema.IsExported = true
pg.GenSchema.DiscriminatorField = dsi.FieldName
for _, v := range dsi.Children {
if pg.GenSchema.Discriminates == nil {
pg.GenSchema.Discriminates = make(map[string]string)
}
pg.GenSchema.Discriminates[v.FieldValue] = v.GoType
}
}
dse, ok := di.Discriminated["#/definitions/"+name]
if ok {
pg.GenSchema.DiscriminatorField = dse.FieldName
pg.GenSchema.DiscriminatorValue = dse.FieldValue
pg.GenSchema.IsSubType = true
// find the referenced definitions
// check if it has a discriminator defined
// when it has a discriminator get the schema and run makeGenSchema for it.
// replace the ref with this new genschema
swsp := specDoc.Spec()
for i, ss := range schema.AllOf {
ref := ss.Ref
for ref.String() != "" {
rsch, err := spec.ResolveRef(swsp, &ref)
if err != nil {
return nil, err
}
ref = rsch.Ref
if rsch != nil && rsch.Ref.String() != "" {
ref = rsch.Ref
continue
}
ref = spec.Ref{}
if rsch != nil && rsch.Discriminator != "" {
gs, err := makeGenDefinitionHierarchy(strings.TrimPrefix(ss.Ref.String(), "#/definitions/"), pkg, pg.GenSchema.Name, *rsch, specDoc, pg.IncludeValidator, pg.IncludeModel)
if err != nil {
return nil, err
}
gs.GenSchema.IsBaseType = true
gs.GenSchema.IsExported = true
pg.GenSchema.AllOf[i] = gs.GenSchema
schPtr := &(pg.GenSchema.AllOf[i])
if schPtr.AdditionalItems != nil {
schPtr.AdditionalItems.IsBaseType = true
}
if schPtr.AdditionalProperties != nil {
schPtr.AdditionalProperties.IsBaseType = true
}
for j := range schPtr.Properties {
schPtr.Properties[j].IsBaseType = true
schPtr.Properties[j].ValueExpression += "()"
}
}
}
}
}
var defaultImports []string
if pg.GenSchema.HasValidations {
defaultImports = []string{
"github.com/go-openapi/errors",
"github.com/go-openapi/runtime",
"github.com/go-openapi/validate",
}
}
var extras []GenSchema
var extraKeys []string
for k := range pg.ExtraSchemas {
//.........這裏部分代碼省略.........
示例15: GenerateClient
// GenerateClient generates a client library for a swagger spec document.
func GenerateClient(name string, modelNames, operationIDs []string, opts GenOpts) error {
defer func() {
typeMapping["binary"] = "io.ReadCloser"
}()
typeMapping["binary"] = "io.Writer"
customFormatters["io.Writer"] = struct{}{}
if opts.TemplateDir != "" {
if err := templates.LoadDir(opts.TemplateDir); err != nil {
return err
}
}
compileTemplates()
// Load the spec
_, specDoc, err := loadSpec(opts.Spec)
if err != nil {
return err
}
analyzed := analysis.New(specDoc.Spec())
models, err := gatherModels(specDoc, modelNames)
if err != nil {
return err
}
operations := gatherOperations(analyzed, operationIDs)
defaultScheme := opts.DefaultScheme
if defaultScheme == "" {
defaultScheme = sHTTP
}
defaultConsumes := opts.DefaultConsumes
if defaultConsumes == "" {
defaultConsumes = runtime.JSONMime
}
defaultProduces := opts.DefaultProduces
if defaultProduces == "" {
defaultProduces = runtime.JSONMime
}
generator := appGenerator{
Name: appNameOrDefault(specDoc, name, "rest"),
SpecDoc: specDoc,
Analyzed: analyzed,
Models: models,
Operations: operations,
Target: opts.Target,
DumpData: opts.DumpData,
Package: mangleName(swag.ToFileName(opts.ClientPackage), "client"),
APIPackage: mangleName(swag.ToFileName(opts.APIPackage), "api"),
ModelsPackage: mangleName(swag.ToFileName(opts.ModelPackage), "definitions"),
ServerPackage: mangleName(swag.ToFileName(opts.ServerPackage), "server"),
ClientPackage: mangleName(swag.ToFileName(opts.ClientPackage), "client"),
Principal: opts.Principal,
DefaultScheme: defaultScheme,
DefaultProduces: defaultProduces,
DefaultConsumes: defaultConsumes,
GenOpts: &opts,
}
generator.Receiver = "o"
return (&clientGenerator{generator}).Generate()
}