Skip to content

Commit

Permalink
Merge 3029028 into 94ff5ed
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Oct 21, 2020
2 parents 94ff5ed + 3029028 commit 38eb20c
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 118 deletions.
6 changes: 0 additions & 6 deletions definition/definition.go
Expand Up @@ -18,7 +18,6 @@ package definition

import (
"context"
"net/http"
"reflect"
)

Expand Down Expand Up @@ -228,11 +227,6 @@ type Definition struct {
// In some cases, succeessful data and error data should be generated in
// different ways.
ErrorProduces []string
// Handler is a http.Handler implementation, nirvana supports to add a http.Handler (e.g. httputil.ReverseProxy)
// directly to the router.
// If this field is not nil, there is no need to set the Function, Parameters, and Results fields.
// See examples/getting-started/handler for a real example.
Handler http.Handler
// Function is a function handler. It must be func type.
Function interface{}
// Parameters describes function parameters.
Expand Down
77 changes: 0 additions & 77 deletions examples/getting-started/handler/main.go

This file was deleted.

1 change: 0 additions & 1 deletion service/builder.go
Expand Up @@ -151,7 +151,6 @@ func (b *builder) copyDefinition(d *definition.Definition, consumes []string, pr
Method: d.Method,
Summary: d.Summary,
Function: d.Function,
Handler: d.Handler,
Description: d.Description,
}
if len(d.Consumes) > 0 {
Expand Down
35 changes: 9 additions & 26 deletions service/executor.go
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io"
"net/http"
"path"
"reflect"
"runtime"
Expand Down Expand Up @@ -64,16 +63,19 @@ func (i *inspector) addDefinition(d definition.Definition) error {
if len(d.ErrorProduces) <= 0 {
return definitionNoErrorProduces.Error(d.Method, i.path)
}
if d.Function == nil && d.Handler == nil {
if d.Function == nil {
return definitionNoFunction.Error(d.Method, i.path)
}

value := reflect.ValueOf(d.Function)
if value.Kind() != reflect.Func {
return definitionInvalidFunctionType.Error(value.Type(), d.Method, i.path)
}
c := &executor{
logger: i.logger,
method: method,
code: HTTPCodeFor(d.Method),
logger: i.logger,
method: method,
code: HTTPCodeFor(d.Method),
function: value,
}

consumeAll := false
consumes := map[string]bool{}
for _, ct := range d.Consumes {
Expand Down Expand Up @@ -118,19 +120,6 @@ func (i *inspector) addDefinition(d definition.Definition) error {
}
}
}

if d.Handler != nil {
c.handler = d.Handler
i.executors[method] = append(i.executors[method], c)
return nil
}

value := reflect.ValueOf(d.Function)
if value.Kind() != reflect.Func {
return definitionInvalidFunctionType.Error(value.Type(), d.Method, i.path)
}
c.function = value

errorProduceAll := false
errorProduces := map[string]bool{}
for _, ct := range d.ErrorProduces {
Expand Down Expand Up @@ -391,7 +380,6 @@ type executor struct {
parameters []parameter
results []result
function reflect.Value
handler http.Handler
}

type parameter struct {
Expand Down Expand Up @@ -450,11 +438,6 @@ func (e *executor) Execute(ctx context.Context) (err error) {
if c == nil {
return noContext.Error()
}
if e.handler != nil {
e.handler.ServeHTTP(c.ResponseWriter(), c.Request())
return nil
}

paramValues := make([]reflect.Value, 0, len(e.parameters))
for _, p := range e.parameters {
result, err := p.generator.Generate(ctx, c.ValueContainer(), e.consumers, p.name, p.targetType)
Expand Down
2 changes: 1 addition & 1 deletion service/utils.go
Expand Up @@ -112,7 +112,7 @@ var definitionNoConsumes = errors.InternalServerError.Build("Nirvana:Service:Def
var definitionNoProduces = errors.InternalServerError.Build("Nirvana:Service:DefinitionNoProduces", "no content type to produce in [${method}]${path}")
var definitionNoErrorProduces = errors.InternalServerError.Build("Nirvana:Service:DefinitionNoErrorProduces",
"no content type to produce error in [${method}]${path}")
var definitionNoFunction = errors.InternalServerError.Build("Nirvana:Service:DefinitionNoFunction", "no function or handler in [${method}]${path}")
var definitionNoFunction = errors.InternalServerError.Build("Nirvana:Service:DefinitionNoFunction", "no function in [${method}]${path}")
var definitionInvalidFunctionType = errors.InternalServerError.Build("Nirvana:Service:DefinitionInvalidFunctionType",
"${type} is not function in [${method}]${path}")

Expand Down
7 changes: 0 additions & 7 deletions utils/api/definitions.go
Expand Up @@ -98,10 +98,6 @@ type Definition struct {

// NewDefinition creates openapi.Definition from definition.Definition.
func NewDefinition(tc *TypeContainer, d *definition.Definition) (*Definition, error) {
if d.Function == nil {
return nil, nil
}

cd := &Definition{
Method: d.Method,
HTTPMethod: service.HTTPMethodFor(d.Method),
Expand Down Expand Up @@ -175,9 +171,6 @@ func NewDefinitions(tc *TypeContainer, definitions []definition.Definition) ([]D
if err != nil {
return nil, err
}
if cd == nil {
continue
}
result[i] = *cd
}
return result, nil
Expand Down

0 comments on commit 38eb20c

Please sign in to comment.