Skip to content

Commit

Permalink
Reorganize functions in files
Browse files Browse the repository at this point in the history
  • Loading branch information
brandur committed Jun 23, 2017
1 parent 60356cd commit 72c4df8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 53 deletions.
82 changes: 41 additions & 41 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,6 @@ type DataGenerator struct {
fixtures *Fixtures
}

func (g *DataGenerator) maybeDereference(schema *JSONSchema) (*JSONSchema, error) {
if schema.Ref != "" {
definition, err := definitionFromJSONPointer(schema.Ref)
if err != nil {
return nil, err
}

newSchema, ok := g.definitions[definition]
if !ok {
return nil, fmt.Errorf("Couldn't dereference: %v", schema.Ref)
}
schema = newSchema
}
return schema, nil
}

func (g *DataGenerator) generateResource(schema *JSONSchema) (interface{}, error) {
if schema.XResourceID == "" {
// Technically type can also be just a string, but we're not going to
// support this for now.
if schema.Type != nil {
for _, schemaType := range schema.Type {
if schemaType == "object" {
return map[string]interface{}{}, nil
}
}
return nil, errNotSupported
}

// Support schemas with no type annotation at all
return map[string]interface{}{}, nil
}

fixture, ok := g.fixtures.Resources[ResourceID(schema.XResourceID)]
if !ok {
return map[string]interface{}{}, nil
}

return fixture, nil
}

func (g *DataGenerator) Generate(schema *JSONSchema, requestPath string, expansions *ExpansionLevel) (interface{}, error) {
return g.generateInternal(schema, requestPath, expansions, nil)
}
Expand Down Expand Up @@ -128,6 +87,47 @@ func (g *DataGenerator) generateInternal(schema *JSONSchema, requestPath string,
return data, nil
}

func (g *DataGenerator) generateResource(schema *JSONSchema) (interface{}, error) {
if schema.XResourceID == "" {
// Technically type can also be just a string, but we're not going to
// support this for now.
if schema.Type != nil {
for _, schemaType := range schema.Type {
if schemaType == "object" {
return map[string]interface{}{}, nil
}
}
return nil, errNotSupported
}

// Support schemas with no type annotation at all
return map[string]interface{}{}, nil
}

fixture, ok := g.fixtures.Resources[ResourceID(schema.XResourceID)]
if !ok {
return map[string]interface{}{}, nil
}

return fixture, nil
}

func (g *DataGenerator) maybeDereference(schema *JSONSchema) (*JSONSchema, error) {
if schema.Ref != "" {
definition, err := definitionFromJSONPointer(schema.Ref)
if err != nil {
return nil, err
}

newSchema, ok := g.definitions[definition]
if !ok {
return nil, fmt.Errorf("Couldn't dereference: %v", schema.Ref)
}
schema = newSchema
}
return schema, nil
}

func (g *DataGenerator) maybeGenerateList(properties map[string]*JSONSchema, existingData interface{}, requestPath string, expansions *ExpansionLevel) (interface{}, error) {
object, ok := properties["object"]
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func main() {
log.Fatalf("Error listening on socket: %v", err)
}

http.HandleFunc("/", stub.handleRequest)
http.HandleFunc("/", stub.HandleRequest)
server := http.Server{}
server.Serve(listener)
}
22 changes: 11 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,7 @@ type stubServerRoute struct {
method *OpenAPIMethod
}

func (s *StubServer) routeRequest(r *http.Request) *OpenAPIMethod {
verbRoutes := s.routes[HTTPVerb(r.Method)]
for _, route := range verbRoutes {
if route.pattern.MatchString(r.URL.Path) {
return route.method
}
}
return nil
}

func (s *StubServer) handleRequest(w http.ResponseWriter, r *http.Request) {
func (s *StubServer) HandleRequest(w http.ResponseWriter, r *http.Request) {
log.Printf("Request: %v %v", r.Method, r.URL.Path)
start := time.Now()

Expand Down Expand Up @@ -149,6 +139,16 @@ func (s *StubServer) initializeRouter() {
numPaths, numEndpoints)
}

func (s *StubServer) routeRequest(r *http.Request) *OpenAPIMethod {
verbRoutes := s.routes[HTTPVerb(r.Method)]
for _, route := range verbRoutes {
if route.pattern.MatchString(r.URL.Path) {
return route.method
}
}
return nil
}

// ---

var pathParameterPattern = regexp.MustCompile(`\{(\w+)\}`)
Expand Down

0 comments on commit 72c4df8

Please sign in to comment.