Skip to content

Commit

Permalink
feat: adds byte and byte array types
Browse files Browse the repository at this point in the history
  • Loading branch information
Sadzeih authored and denouche committed Sep 25, 2019
1 parent 5a84632 commit 1c89fea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions docparser/datatest/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type Pet struct {
StrData map[string]string `json:"strData"`
Children map[string]Pet `json:"children"`
IntData map[string]int `json:"IntData"`
ByteData []byte `json:"ByteData"`
}

// Foo struct
Expand Down
13 changes: 13 additions & 0 deletions docparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func parseNamedType(gofile *ast.File, expr ast.Expr) (*schema, error) {
return t, nil
case *ast.ArrayType: // slice type
cp, _ := parseNamedType(gofile, ftpe.Elt)
if cp.Format == "binary" {
p.Type = "string"
p.Format = "binary"
return &p, nil
}
p.Type = "array"
p.Items = map[string]string{}
if cp.Type != "" {
Expand Down Expand Up @@ -135,17 +140,25 @@ func parseIdentProperty(expr *ast.Ident) (t, format string, err error) {
t = "string"
case "int":
t = "integer"
case "int8":
t = "integer"
format = "int8"
case "int64":
t = "integer"
format = "int64"
case "int32":
t = "integer"
format = "int32"
case "time":
t = "string"
format = "date-time"
case "float64":
t = "number"
case "bool":
t = "boolean"
case "byte":
t = "string"
format = "binary"
default:
t = expr.Name
err = fmt.Errorf("Can't set the type %s", expr.Name)
Expand Down
24 changes: 18 additions & 6 deletions docparser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func TestParseNamedType(t *testing.T) {
expr: &ast.Ident{Name: "time"},
expectedSchema: &schema{Type: "string", Format: "date-time"},
},
{
description: "Should parse *ast.Ident with name byte",
expr: &ast.Ident{Name: "byte"},
expectedSchema: &schema{Type: "string", Format: "binary"},
},
{
description: "Should parse *ast.StarExpr and set Nullable",
expr: &ast.StarExpr{X: &ast.Ident{Name: "time"}},
Expand All @@ -111,6 +116,11 @@ func TestParseNamedType(t *testing.T) {
"type": "string",
}},
},
{
description: "Should parse *ast.ArrayType with byte type",
expr: &ast.ArrayType{Elt: &ast.Ident{Name: "byte"}},
expectedSchema: &schema{Type: "string", Format: "binary"},
},
{
description: "Should parse *ast.ArrayType with unknown type",
expr: &ast.ArrayType{Elt: &ast.Ident{Name: "unknown"}},
Expand Down Expand Up @@ -422,14 +432,16 @@ func TestParseIdentProperty(t *testing.T) {
expectedType: "integer",
},
{
description: "parse int64 ident type",
expr: &ast.Ident{Name: "int64"},
expectedType: "integer",
description: "parse int64 ident type",
expr: &ast.Ident{Name: "int64"},
expectedType: "integer",
expectedFormat: "int64",
},
{
description: "parse int32 ident type",
expr: &ast.Ident{Name: "int32"},
expectedType: "integer",
description: "parse int32 ident type",
expr: &ast.Ident{Name: "int32"},
expectedType: "integer",
expectedFormat: "int32",
},
{
description: "parse time ident type",
Expand Down

0 comments on commit 1c89fea

Please sign in to comment.