github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/toolkit/openapi/v3/model.go (about)

     1  package v3
     2  
     3  import "io"
     4  
     5  // Contact https://spec.openapis.org/oas/v3.0.3#contact-object
     6  type Contact struct {
     7  	Email string `json:"email,omitempty"`
     8  }
     9  
    10  // License https://spec.openapis.org/oas/v3.0.3#license-object
    11  type License struct {
    12  	Name string `json:"name,omitempty"`
    13  	URL  string `json:"url,omitempty"`
    14  }
    15  
    16  // Info https://spec.openapis.org/oas/v3.0.3#info-object
    17  type Info struct {
    18  	Title          string   `json:"title,omitempty"`
    19  	Description    string   `json:"description,omitempty"`
    20  	TermsOfService string   `json:"termsOfService,omitempty"`
    21  	Contact        *Contact `json:"contact,omitempty"`
    22  	License        *License `json:"license,omitempty"`
    23  	Version        string   `json:"version,omitempty"`
    24  }
    25  
    26  // Server https://spec.openapis.org/oas/v3.0.3#server-object
    27  type Server struct {
    28  	URL string `json:"url,omitempty"`
    29  }
    30  
    31  // ExternalDocs https://spec.openapis.org/oas/v3.0.3#external-documentation-object
    32  type ExternalDocs struct {
    33  	Description string `json:"description,omitempty"`
    34  	URL         string `json:"url,omitempty"`
    35  }
    36  
    37  // Tag https://spec.openapis.org/oas/v3.0.3#tag-object
    38  type Tag struct {
    39  	Name         string        `json:"name,omitempty"`
    40  	Description  string        `json:"description,omitempty"`
    41  	ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"`
    42  }
    43  
    44  // In represents parameter position
    45  type In string
    46  
    47  const (
    48  	// InQuery query string parameter
    49  	InQuery In = "query"
    50  	// InPath TODO not implemented yet
    51  	InPath In = "path"
    52  	// InHeader TODO not implemented yet
    53  	InHeader In = "header"
    54  	// InCookie TODO not implemented yet
    55  	InCookie In = "cookie"
    56  )
    57  
    58  // Example https://spec.openapis.org/oas/v3.0.3#example-object
    59  type Example struct {
    60  	// TODO
    61  }
    62  
    63  // Encoding https://spec.openapis.org/oas/v3.0.3#encoding-object
    64  type Encoding struct {
    65  	// TODO
    66  }
    67  
    68  // MediaType https://spec.openapis.org/oas/v3.0.3#media-type-object
    69  type MediaType struct {
    70  	Schema   *Schema             `json:"schema,omitempty"`
    71  	Example  interface{}         `json:"example,omitempty"`
    72  	Examples map[string]Example  `json:"examples,omitempty"`
    73  	Encoding map[string]Encoding `json:"encoding,omitempty"`
    74  }
    75  
    76  // Content REQUIRED. The content of the request body. The key is a media type or [media type range]appendix-D)
    77  // and the value describes it. For requests that match multiple keys, only the most specific key is applicable.
    78  // e.g. text/plain overrides text/*
    79  type Content struct {
    80  	TextPlain *MediaType `json:"text/plain,omitempty"`
    81  	JSON      *MediaType `json:"application/json,omitempty"`
    82  	FormURL   *MediaType `json:"application/x-www-form-urlencoded,omitempty"`
    83  	Stream    *MediaType `json:"application/octet-stream,omitempty"`
    84  	FormData  *MediaType `json:"multipart/form-data,omitempty"`
    85  	Default   *MediaType `json:"*/*,omitempty"`
    86  }
    87  
    88  // Parameter https://spec.openapis.org/oas/v3.0.3#parameter-object
    89  type Parameter struct {
    90  	Name            string      `json:"name,omitempty"`
    91  	In              In          `json:"in,omitempty"`
    92  	Description     string      `json:"description,omitempty"`
    93  	Required        bool        `json:"required,omitempty"`
    94  	Deprecated      bool        `json:"deprecated,omitempty"`
    95  	Example         interface{} `json:"example,omitempty"`
    96  	Schema          *Schema     `json:"schema,omitempty"`
    97  	Style           string      `json:"style,omitempty"`
    98  	Explode         bool        `json:"explode,omitempty"`
    99  	AllowReserved   bool        `json:"allowReserved,omitempty"`
   100  	Content         *Content    `json:"content,omitempty"`
   101  	AllowEmptyValue bool        `json:"allowEmptyValue,omitempty"`
   102  }
   103  
   104  // RequestBody https://spec.openapis.org/oas/v3.0.3#request-body-object
   105  type RequestBody struct {
   106  	Description string   `json:"description,omitempty"`
   107  	Content     *Content `json:"content,omitempty"`
   108  	Required    bool     `json:"required,omitempty"`
   109  	Ref         string   `json:"$ref,omitempty"`
   110  }
   111  
   112  // Header https://spec.openapis.org/oas/v3.0.3#header-object
   113  type Header struct {
   114  	Ref         string      `json:"$ref,omitempty"`
   115  	Description string      `json:"description,omitempty"`
   116  	Required    bool        `json:"required,omitempty"`
   117  	Deprecated  bool        `json:"deprecated,omitempty"`
   118  	Example     interface{} `json:"example,omitempty"`
   119  	Schema      *Schema     `json:"schema,omitempty"`
   120  }
   121  
   122  // Link https://spec.openapis.org/oas/v3.0.3#link-object
   123  type Link struct {
   124  	// TODO
   125  }
   126  
   127  // Response https://spec.openapis.org/oas/v3.0.3#response-object
   128  type Response struct {
   129  	Description string   `json:"description"`
   130  	Content     *Content `json:"content,omitempty"`
   131  	// TODO
   132  	Headers map[string]Header `json:"headers,omitempty"`
   133  	Links   map[string]Link   `json:"links,omitempty"`
   134  	Ref     string            `json:"$ref,omitempty"`
   135  }
   136  
   137  // Responses https://spec.openapis.org/oas/v3.0.3#responses-object
   138  type Responses struct {
   139  	Resp200 *Response `json:"200,omitempty"`
   140  	Resp400 *Response `json:"400,omitempty"`
   141  	Resp401 *Response `json:"401,omitempty"`
   142  	Resp403 *Response `json:"403,omitempty"`
   143  	Resp404 *Response `json:"404,omitempty"`
   144  	Resp405 *Response `json:"405,omitempty"`
   145  	Default *Response `json:"default,omitempty"`
   146  }
   147  
   148  // Callback https://spec.openapis.org/oas/v3.0.3#callback-object
   149  type Callback struct {
   150  	// TODO
   151  }
   152  
   153  // Security not implemented yet
   154  type Security struct {
   155  	// TODO
   156  }
   157  
   158  // Operation https://spec.openapis.org/oas/v3.0.3#operation-object
   159  type Operation struct {
   160  	Tags         []string            `json:"tags,omitempty"`
   161  	Summary      string              `json:"summary,omitempty"`
   162  	Description  string              `json:"description,omitempty"`
   163  	OperationID  string              `json:"operationId,omitempty"`
   164  	Parameters   []Parameter         `json:"parameters,omitempty"`
   165  	RequestBody  *RequestBody        `json:"requestBody,omitempty"`
   166  	Responses    *Responses          `json:"responses,omitempty"`
   167  	Deprecated   bool                `json:"deprecated,omitempty"`
   168  	ExternalDocs *ExternalDocs       `json:"externalDocs,omitempty"`
   169  	Callbacks    map[string]Callback `json:"callbacks,omitempty"`
   170  	Security     []Security          `json:"security,omitempty"`
   171  	Servers      []Server            `json:"servers,omitempty"`
   172  }
   173  
   174  // Path https://spec.openapis.org/oas/v3.0.3#path-item-object
   175  type Path struct {
   176  	Get    *Operation `json:"get,omitempty"`
   177  	Post   *Operation `json:"post,omitempty"`
   178  	Put    *Operation `json:"put,omitempty"`
   179  	Delete *Operation `json:"delete,omitempty"`
   180  	// TODO
   181  	Parameters []Parameter `json:"parameters,omitempty"`
   182  }
   183  
   184  // SecurityScheme https://spec.openapis.org/oas/v3.0.3#security-scheme-object
   185  type SecurityScheme struct {
   186  	// TODO
   187  }
   188  
   189  // Discriminator https://spec.openapis.org/oas/v3.0.3#discriminator-object
   190  type Discriminator struct {
   191  	PropertyName string            `json:"propertyName,omitempty"`
   192  	Mapping      map[string]string `json:"mapping,omitempty"`
   193  }
   194  
   195  // Schema https://spec.openapis.org/oas/v3.0.3#schema-object
   196  type Schema struct {
   197  	Ref              string             `json:"$ref,omitempty"`
   198  	Title            string             `json:"title,omitempty"`
   199  	Type             Type               `json:"type,omitempty"`
   200  	Properties       map[string]*Schema `json:"properties,omitempty"`
   201  	Format           Format             `json:"format,omitempty"`
   202  	Items            *Schema            `json:"items,omitempty"`
   203  	Description      string             `json:"description,omitempty"`
   204  	Default          interface{}        `json:"default,omitempty"`
   205  	Example          interface{}        `json:"example,omitempty"`
   206  	Deprecated       bool               `json:"deprecated,omitempty"`
   207  	Discriminator    *Discriminator     `json:"discriminator,omitempty"`
   208  	Nullable         bool               `json:"nullable,omitempty"`
   209  	Maximum          interface{}        `json:"maximum,omitempty"`
   210  	Minimum          interface{}        `json:"minimum,omitempty"`
   211  	ExclusiveMaximum interface{}        `json:"exclusiveMaximum,omitempty"`
   212  	ExclusiveMinimum interface{}        `json:"exclusiveMinimum,omitempty"`
   213  	MaxLength        int                `json:"maxLength,omitempty"`
   214  	MinLength        int                `json:"minLength,omitempty"`
   215  	Required         []string           `json:"required,omitempty"`
   216  	Enum             []interface{}      `json:"enum,omitempty"`
   217  	AllOf            []*Schema          `json:"allOf,omitempty"`
   218  	OneOf            []*Schema          `json:"oneOf,omitempty"`
   219  	AnyOf            []*Schema          `json:"anyOf,omitempty"`
   220  	Not              []*Schema          `json:"not,omitempty"`
   221  	// AdditionalProperties *Schema or bool
   222  	AdditionalProperties interface{} `json:"additionalProperties,omitempty"`
   223  	Pattern              interface{} `json:"pattern,omitempty"`
   224  }
   225  
   226  // Components https://spec.openapis.org/oas/v3.0.3#components-object
   227  type Components struct {
   228  	Schemas       map[string]Schema      `json:"schemas,omitempty"`
   229  	RequestBodies map[string]RequestBody `json:"requestBodies,omitempty"`
   230  	Responses     map[string]Response    `json:"responses,omitempty"`
   231  	// TODO
   232  	Parameters map[string]Parameter `json:"parameters,omitempty"`
   233  	// TODO
   234  	Examples map[string]Example `json:"examples,omitempty"`
   235  	// TODO
   236  	Headers map[string]Header `json:"headers,omitempty"`
   237  	// TODO
   238  	SecuritySchemes map[string]SecurityScheme `json:"securitySchemes,omitempty"`
   239  	// TODO
   240  	Links map[string]Link `json:"links,omitempty"`
   241  	// TODO
   242  	Callbacks map[string]Callback `json:"callbacks,omitempty"`
   243  }
   244  
   245  // API https://spec.openapis.org/oas/v3.0.3#openapi-object
   246  type API struct {
   247  	Openapi      string          `json:"openapi,omitempty"`
   248  	Info         *Info           `json:"info,omitempty"`
   249  	Servers      []Server        `json:"servers,omitempty"`
   250  	Tags         []Tag           `json:"tags,omitempty"`
   251  	Paths        map[string]Path `json:"paths,omitempty"`
   252  	Components   *Components     `json:"components,omitempty"`
   253  	ExternalDocs *ExternalDocs   `json:"externalDocs,omitempty"`
   254  }
   255  
   256  // Type represents types in OpenAPI3.0 spec
   257  type Type string
   258  
   259  const (
   260  	// IntegerT integer
   261  	IntegerT Type = "integer"
   262  	// StringT string
   263  	StringT Type = "string"
   264  	// BooleanT boolean
   265  	BooleanT Type = "boolean"
   266  	// NumberT number
   267  	NumberT Type = "number"
   268  	// ObjectT object
   269  	ObjectT Type = "object"
   270  	// ArrayT array
   271  	ArrayT Type = "array"
   272  )
   273  
   274  // Format represents format in OpenAPI3.0 spec
   275  type Format string
   276  
   277  const (
   278  	// Int32F int32
   279  	Int32F Format = "int32"
   280  	// Int64F int64
   281  	Int64F Format = "int64"
   282  	// FloatF float
   283  	FloatF Format = "float"
   284  	// DoubleF double
   285  	DoubleF Format = "double"
   286  	// DateTimeF date-time
   287  	DateTimeF Format = "date-time"
   288  	// BinaryF binary
   289  	BinaryF Format = "binary"
   290  )
   291  
   292  var (
   293  	// Any constant schema for object
   294  	Any = &Schema{
   295  		Type: ObjectT,
   296  	}
   297  	// Int constant schema for int
   298  	Int = &Schema{
   299  		Type:   IntegerT,
   300  		Format: Int32F,
   301  	}
   302  	// Int64 constant schema for int64
   303  	Int64 = &Schema{
   304  		Type:   IntegerT,
   305  		Format: Int64F,
   306  	}
   307  	// String constant schema for string
   308  	String = &Schema{
   309  		Type: StringT,
   310  	}
   311  	// Time constant schema for time
   312  	Time = &Schema{
   313  		Type:   StringT,
   314  		Format: DateTimeF,
   315  	}
   316  	// Bool constant schema for bool
   317  	Bool = &Schema{
   318  		Type: BooleanT,
   319  	}
   320  	// Float32 constant schema for float32
   321  	Float32 = &Schema{
   322  		Type:   NumberT,
   323  		Format: FloatF,
   324  	}
   325  	// Float64 constant schema for float64
   326  	Float64 = &Schema{
   327  		Type:   NumberT,
   328  		Format: DoubleF,
   329  	}
   330  	// File constant schema for file
   331  	File = &Schema{
   332  		Type:   StringT,
   333  		Format: BinaryF,
   334  	}
   335  	// FileArray constant schema for file slice
   336  	FileArray = &Schema{
   337  		Type:  ArrayT,
   338  		Items: File,
   339  	}
   340  )
   341  
   342  type FileModel struct {
   343  	Filename string
   344  	Reader   io.ReadCloser
   345  }
   346  
   347  func (f *FileModel) Close() error {
   348  	return f.Reader.Close()
   349  }
   350  
   351  type IEnum interface {
   352  	StringSetter(value string)
   353  	StringGetter() string
   354  	UnmarshalJSON(bytes []byte) error
   355  	MarshalJSON() ([]byte, error)
   356  }
   357  
   358  var IEnumMethods = []string{
   359  	"func StringSetter(value string)",
   360  	"func StringGetter() string",
   361  	"func UnmarshalJSON(bytes []byte) error",
   362  	"func MarshalJSON() ([]byte, error)",
   363  }