http urls monitor.

property.go 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package swag
  2. import (
  3. "fmt"
  4. "go/ast"
  5. "strings"
  6. )
  7. type propertyName struct {
  8. SchemaType string
  9. ArrayType string
  10. }
  11. func parseFieldSelectorExpr(astTypeSelectorExpr *ast.SelectorExpr) propertyName {
  12. // TODO: In the future, add functions and make them solve for each user
  13. // Support for time.Time as a structure field
  14. if "Time" == astTypeSelectorExpr.Sel.Name {
  15. return propertyName{SchemaType: "string", ArrayType: "string"}
  16. }
  17. // Support bson.ObjectId type
  18. if "ObjectId" == astTypeSelectorExpr.Sel.Name {
  19. return propertyName{SchemaType: "string", ArrayType: "string"}
  20. }
  21. // Supprt UUID
  22. if "UUID" == strings.ToUpper(astTypeSelectorExpr.Sel.Name) {
  23. return propertyName{SchemaType: "string", ArrayType: "string"}
  24. }
  25. // Supprt shopspring/decimal
  26. if "Decimal" == astTypeSelectorExpr.Sel.Name {
  27. return propertyName{SchemaType: "number", ArrayType: "string"}
  28. }
  29. fmt.Printf("%s is not supported. but it will be set with string temporary. Please report any problems.", astTypeSelectorExpr.Sel.Name)
  30. return propertyName{SchemaType: "string", ArrayType: "string"}
  31. }
  32. // getPropertyName returns the string value for the given field if it exists, otherwise it panics.
  33. // allowedValues: array, boolean, integer, null, number, object, string
  34. func getPropertyName(field *ast.Field) propertyName {
  35. if astTypeSelectorExpr, ok := field.Type.(*ast.SelectorExpr); ok {
  36. return parseFieldSelectorExpr(astTypeSelectorExpr)
  37. }
  38. if astTypeIdent, ok := field.Type.(*ast.Ident); ok {
  39. name := astTypeIdent.Name
  40. schemeType := TransToValidSchemeType(name)
  41. return propertyName{SchemaType: schemeType, ArrayType: schemeType}
  42. }
  43. if ptr, ok := field.Type.(*ast.StarExpr); ok {
  44. if astTypeSelectorExpr, ok := ptr.X.(*ast.SelectorExpr); ok {
  45. return parseFieldSelectorExpr(astTypeSelectorExpr)
  46. }
  47. if astTypeIdent, ok := ptr.X.(*ast.Ident); ok {
  48. name := astTypeIdent.Name
  49. schemeType := TransToValidSchemeType(name)
  50. return propertyName{SchemaType: schemeType, ArrayType: schemeType}
  51. }
  52. if astTypeArray, ok := ptr.X.(*ast.ArrayType); ok { // if array
  53. if astTypeArrayIdent := astTypeArray.Elt.(*ast.Ident); ok {
  54. name := astTypeArrayIdent.Name
  55. return propertyName{SchemaType: "array", ArrayType: name}
  56. }
  57. }
  58. }
  59. if astTypeArray, ok := field.Type.(*ast.ArrayType); ok { // if array
  60. if astTypeArrayExpr, ok := astTypeArray.Elt.(*ast.StarExpr); ok {
  61. if astTypeArrayIdent := astTypeArrayExpr.X.(*ast.Ident); ok {
  62. name := astTypeArrayIdent.Name
  63. return propertyName{SchemaType: "array", ArrayType: name}
  64. }
  65. }
  66. str := fmt.Sprintf("%s", astTypeArray.Elt)
  67. return propertyName{SchemaType: "array", ArrayType: str}
  68. }
  69. if _, ok := field.Type.(*ast.MapType); ok { // if map
  70. //TODO: support map
  71. return propertyName{SchemaType: "object", ArrayType: "object"}
  72. }
  73. if _, ok := field.Type.(*ast.StructType); ok { // if struct
  74. return propertyName{SchemaType: "object", ArrayType: "object"}
  75. }
  76. if _, ok := field.Type.(*ast.InterfaceType); ok { // if interface{}
  77. return propertyName{SchemaType: "object", ArrayType: "object"}
  78. }
  79. panic("not supported" + fmt.Sprint(field.Type))
  80. }