Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for non primitive data type #10

Merged
merged 2 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,20 @@ func generateStructs(swagger *openapi3.Swagger) error {
}

func getType(schema *openapi3.SchemaRef) string {

if schema.Ref != "" {
return strings.Split(schema.Ref, "/")[3]
}
if len(schema.Value.OneOf) > 0 ||
len(schema.Value.AnyOf) > 0 ||
len(schema.Value.AllOf) > 0 {
// TODO: (by bxcodec)
// It's hard to define if it comes to this kind of data:
// - oneOf
// - allOf
// - anyOf
// Just return an plain interface{} let the developer decide later what should it best to this data types
return "interface{}"
}

switch schema.Value.Type {
case "integer":
Expand All @@ -68,7 +78,26 @@ func getType(schema *openapi3.SchemaRef) string {
}
return "string"
case "object":
return "interface{}"
// TODO: (by bxcodec)
// This section temporary I just send map[string]interface{}
// Based on the condition that I believe, if it was an embedded object:
// For example, this Article schema.
// ```
// Article:
// properties:
// publisher:
// type: object
// properties:
// id:
// type: string
// name:
// type: string
// ```
// That means, that publisher's object is not necesessary to have in the code as a struct.
// If it important to have as a struct it must defined using `$ref` then.
// But I'm a bit confused to use between interface{} or explicitly using map[string]interface{} will decide later
// after a real test
return "map[string]interface{}"
case "array":
if items := schema.Value.Items; items != nil {
return fmt.Sprintf("[]%s", getType(items))
Expand Down
26 changes: 23 additions & 3 deletions menekel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ components:
title:
type: string
tag:
type: array
items:
type: string
$ref: "#/components/schemas/Tag"
author:
$ref: "#/components/schemas/Author"
created_at:
Expand All @@ -113,6 +111,28 @@ components:
updated_at:
type: string
format: date
publisher:
type: object
properties:
id:
type: string
name:
type: string

Tag:
properties:
type:
type: string
content:
oneOf:
- type: string
- $ref: "#/components/schemas/Topic"
Topic:
properties:
id:
type: string
name:
type: string
Author:
properties:
id:
Expand Down