Skip to content

Commit

Permalink
feat: support for non primitive data type (#10)
Browse files Browse the repository at this point in the history
* resolving non primitive data type
  • Loading branch information
bxcodec authored Jan 21, 2019
1 parent 85d905a commit ba4a1e4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
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

0 comments on commit ba4a1e4

Please sign in to comment.