rclone/vendor/github.com/yunify/qingstor-sdk-go/template/shared.tmpl
2017-09-30 15:27:27 +01:00

392 lines
12 KiB
Cheetah

{{define "Type"}}
{{- $typeName := index . 0 -}}
{{- $disablePointer := index . 1 -}}
{{- if eq $typeName "string" -}}
{{- if not $disablePointer -}}*{{- end -}}string
{{- else if eq $typeName "boolean" -}}
{{- if not $disablePointer -}}*{{- end -}}bool
{{- else if eq $typeName "integer" -}}
{{- if not $disablePointer -}}*{{- end -}}int
{{- else if eq $typeName "long" -}}
{{- if not $disablePointer -}}*{{- end -}}int64
{{- else if eq $typeName "timestamp" -}}
{{- if not $disablePointer -}}*{{- end -}}time.Time
{{- else if eq $typeName "binary" -}}
io.Reader
{{- else if eq $typeName "array" -}}
interface{}
{{- else if eq $typeName "object" -}}
interface{}
{{- else if eq $typeName "map" -}}
interface{}
{{- else if eq $typeName "any" -}}
interface{}
{{- else -}}
*{{$typeName | camelCase}}Type
{{- end -}}
{{end}}
{{define "PropertyType"}}
{{- $property := index . 0 -}}
{{- $disablePointer := index . 1 -}}
{{- if eq $property.Type "object" -}}
{{template "Type" passThrough $property.ExtraType $disablePointer}}
{{- else if eq $property.Type "array" -}}
[]{{template "Type" passThrough $property.ExtraType $disablePointer}}
{{- else if eq $property.Type "map" -}}
map[string]{{template "Type" passThrough $property.ExtraType $disablePointer}}
{{- else if eq $property.Type "any" -}}
{{template "Type" passThrough $property.Type $disablePointer}}
{{- else -}}
{{template "Type" passThrough $property.Type $disablePointer}}
{{- end -}}
{{end}}
{{define "PropertyTags"}}
{{- $property := . -}}
{{- if $property.IsRequired -}}
{{- printf `json:"%s"` ($property.Name | normalized) -}}
{{- else -}}
{{- printf `json:"%s,omitempty"` ($property.Name | normalized) -}}
{{- end -}}
{{- printf ` name:"%s"` ($property.Name | normalized) -}}
{{- if $property.Format}}
{{- printf ` format:"%s"` $property.Format -}}
{{- end -}}
{{- if $property.Default -}}
{{- printf ` default:"%s"` $property.Default -}}
{{- end -}}
{{end}}
{{define "PropertyTagsDashConnected"}}
{{- $property := . -}}
{{- printf `json:"%s"` ($property.Name | dashConnected) -}}
{{- printf ` name:"%s"` ($property.Name | dashConnected) -}}
{{end}}
{{define "PropertyExtraTags"}}
{{- $propertyExtraTags := . -}}
{{- if $propertyExtraTags -}}
{{- printf " %s" $propertyExtraTags -}}
{{- end -}}
{{end}}
{{define "RenderProperties"}}
{{- $customizedType := index . 0 -}}
{{- $propertyExtraTags := index . 1 -}}
{{- $operationName := index . 2 -}}
{{range $_, $property := $customizedType.Properties -}}
{{if or (ne $operationName "Delete Multiple Objects") (ne $property.ID "Content-MD5") -}}
{{if $property.Description -}}
// {{$property.Description}}
{{end -}}
{{if $property.Enum -}}
// {{$property.ID | camelCase}}'s available values: {{$property.Enum | commaConnected}}
{{end -}}
{{$property.ID | camelCase | upperFirst}}{{" " -}}
{{template "PropertyType" passThrough $property false}}{{" " -}}
`{{template "PropertyTags" $property}}{{template "PropertyExtraTags" $propertyExtraTags}}`{{" " -}}
{{if $property.IsRequired -}}
// Required
{{- end}}
{{- end}}
{{end}}
{{end}}
{{define "RenderOperation"}}
{{$service := index . 0}}
{{$operation := index . 1}}
{{$belongs := replace $service.Name "QingStor" "Service" -1}}
{{$belongs := replace $belongs "Object" "Bucket" -1}}
{{$opID := $operation.ID | camelCase}}
{{$isBucket := eq $service.Name "Bucket"}}
{{$isObject := eq $service.Name "Object"}}
{{$hasQuery := gt (len $operation.Request.Query.Properties) 0}}
{{$hasHeaders := gt (len $operation.Request.Headers.Properties) 0}}
{{$hasElements := gt (len $operation.Request.Elements.Properties) 0}}
{{$hasStringBody := eq $operation.Request.Body.Type "string"}}
{{$hasBinaryBody := eq $operation.Request.Body.Type "binary"}}
{{$hasInput := or $hasQuery $hasHeaders $hasElements $hasStringBody $hasBinaryBody}}
{{if $operation.Description -}}
{{if eq $belongs "Bucket" -}}
// {{replace $opID "Bucket" "" -1}} does {{$operation.Description}}
{{else -}}
// {{$opID}} does {{$operation.Description}}
{{end -}}
{{end -}}
{{if $operation.DocumentationURL -}}
// Documentation URL: {{$operation.DocumentationURL}}
{{- end}}
{{if eq $belongs "Bucket" -}}
func (s *{{$belongs}}) {{replace $opID "Bucket" "" -1 -}}(
{{- if $isObject}}objectKey string,{{end -}}
{{- if $hasInput}}input *{{$opID}}Input{{end -}}
) (*{{$opID}}Output, error) {
{{else -}}
func (s *{{$belongs}}) {{$opID}}(
{{- if $hasInput}}input *{{$opID}}Input{{end -}}
) (*{{$opID}}Output, error) {
{{end -}}
{{if eq $belongs "Bucket" -}}
r, x, err := s.{{replace $opID "Bucket" "" -1}}Request(
{{- if $isObject}}objectKey,{{end -}}
{{- if $hasInput}}input{{end -}}
)
{{else -}}
r, x, err := s.{{$opID}}Request(
{{- if $hasInput}}input{{end -}}
)
{{end}}
if err != nil {
return x, err
}
err = r.Send()
if err != nil {
return nil, err
}
requestID := r.HTTPResponse.Header.Get(http.CanonicalHeaderKey("X-QS-Request-ID"))
x.RequestID = &requestID
return x, err
}
{{if $operation.Description -}}
{{if eq $belongs "Bucket" -}}
// {{replace $opID "Bucket" "" -1}}Request creates request and output object of {{$opID}}.
{{else -}}
// {{$opID}}Request creates request and output object of {{$opID}}.
{{end -}}
{{end -}}
{{if eq $belongs "Bucket" -}}
func (s *{{$belongs}}) {{replace $opID "Bucket" "" -1 -}}Request(
{{- if $isObject}}objectKey string,{{end -}}
{{- if $hasInput}}input *{{$opID}}Input{{end -}}
) (*request.Request, *{{$opID}}Output, error) {
{{else -}}
func (s *{{$belongs}}) {{$opID}}Request(
{{- if $hasInput}}input *{{$opID}}Input{{end -}}
) (*request.Request, *{{$opID}}Output, error) {
{{end -}}
{{if $hasInput}}
if input == nil {
input = &{{$opID}}Input{}
}
{{end}}
{{$path := $operation.Request.Path}}
{{$path := replace $path "{" "<" -1}}
{{$path := replace $path "}" ">" -1}}
{{$path := dashConnected $path}}
{{- if ne $belongs "Service"}}
properties := *s.Properties
{{- end}}
{{if eq $service.Name "Object"}}
properties.ObjectKey = &objectKey
{{end}}
o := &data.Operation{
Config: s.Config,
{{- if ne $belongs "Service"}}
Properties: &properties,
{{- end}}
APIName: "{{$operation.Name}}",
RequestMethod: "{{$operation.Request.Method}}",
RequestURI: "{{$path}}",
StatusCodes: []int{
{{range $keyStatus, $valueStatus := $operation.Responses -}}
{{- if $valueStatus.StatusCode -}}
{{$valueStatus.StatusCode.Code}}, // {{$valueStatus.StatusCode.Description}}
{{else}}
200, // OK
{{end -}}
{{else}}
200, // OK
{{end -}}
},
}
x := &{{$opID}}Output{}
r, err := request.New(o, {{if $hasInput}}input{{else}}nil{{end}}, x)
if err != nil {
return nil, nil, err
}
return r, x, nil
}
{{if $hasInput}}
// {{$opID}}Input presents input for {{$opID}}.
type {{$opID}}Input struct {
{{- if $operation.Request.Query.Properties | len}}
{{$data := $operation.Request.Query -}}
{{template "RenderProperties" passThrough $data `location:"query"` $operation.Name}}
{{end}}
{{- if $operation.Request.Headers.Properties | len}}
{{$data := $operation.Request.Headers -}}
{{template "RenderProperties" passThrough $data `location:"headers"` $operation.Name}}
{{end}}
{{- if $operation.Request.Elements.Properties | len}}
{{$data := $operation.Request.Elements -}}
{{template "RenderProperties" passThrough $data `location:"elements"` $operation.Name}}
{{end}}
{{- if eq $operation.Request.Body.Type "string"}}
{{if $operation.Request.Body.Description -}}
// {{$operation.Request.Body.Description}}
{{- end}}
Body string `location:"body"`
{{else if eq $operation.Request.Body.Type "binary"}}
{{if $operation.Request.Body.Description -}}
// {{$operation.Request.Body.Description}}
{{- end}}
Body io.Reader `location:"body"`
{{end}}
}
// Validate validates the input for {{$opID}}.
func (v *{{$opID}}Input) Validate() error {
{{template "ValidateCustomizedType" passThrough $operation.Request.Query $operation.Name}}
{{template "ValidateCustomizedType" passThrough $operation.Request.Headers $operation.Name}}
{{template "ValidateCustomizedType" passThrough $operation.Request.Elements $operation.Name}}
return nil
}
{{end}}
// {{$opID}}Output presents output for {{$opID}}.
type {{$opID}}Output struct {
StatusCode *int `location:"statusCode"`
RequestID *string `location:"requestID"`
{{range $keyStatus, $valueStatus := $operation.Responses -}}
{{if eq $valueStatus.Body.Type "string"}}
{{if $valueStatus.Body.Description -}}
// {{$valueStatus.Body.Description}}
{{- end}}
Body string `location:"body"`
{{else if eq $valueStatus.Body.Type "binary"}}
{{if $valueStatus.Body.Description -}}
// {{$valueStatus.Body.Description}}
{{- end}}
Body io.ReadCloser `location:"body"`
{{end}}
{{if $valueStatus.Elements.Properties | len}}
{{$data := $valueStatus.Elements}}
{{template "RenderProperties" passThrough $data `location:"elements"` $operation.Name}}
{{end}}
{{if $valueStatus.Headers.Properties | len}}
{{$data := $valueStatus.Headers}}
{{template "RenderProperties" passThrough $data `location:"headers"` $operation.Name}}
{{end}}
{{end}}
}
{{end}}
{{define "SubServiceInitParams"}}
{{- $customizedType := index . 0 -}}
{{- $disablePointer := index . 1 -}}
{{- range $_, $property := $customizedType.Properties -}}
{{$property.ID | camelCase | lowerFirstWord}}{{" " -}}
{{template "PropertyType" passThrough $property $disablePointer}},
{{- end -}}
{{end}}
{{define "ValidateCustomizedType"}}
{{$customizedType := index . 0}}
{{$operationName := index . 1}}
{{range $_, $property := $customizedType.Properties}}
{{if or (ne $operationName "Delete Multiple Objects") (ne $property.ID "Content-MD5") -}}
{{$isNormalType := or (eq $property.Type "string") (eq $property.Type "integer")}}
{{$isContentLength := eq $property.ID "Content-Length"}}
{{if and $isNormalType (not $isContentLength) }}
{{if $property.IsRequired }}
if v.{{$property.ID | camelCase}} == nil {
return errors.ParameterRequiredError{
ParameterName: "{{$property.ID | camelCase}}",
ParentName: "{{$customizedType.ID | camelCase}}",
}
}
{{end}}
{{$parameterName := $property.ID | camelCase | lowerFirstWord}}
{{if gt ($property.Enum | len) 0}}
if v.{{$property.ID | camelCase}} != nil {
{{$parameterName}}ValidValues := []string{
{{- $property.Enum | commaConnectedWithQuote -}}
}
{{$parameterName}}ParameterValue := fmt.Sprint(*v.{{$property.ID | camelCase}})
{{$parameterName}}IsValid := false
for _, value := range {{$parameterName}}ValidValues {
if value == {{$parameterName}}ParameterValue {
{{$parameterName}}IsValid = true
}
}
if !{{$parameterName}}IsValid {
return errors.ParameterValueNotAllowedError{
ParameterName: "{{$property.ID | camelCase}}",
ParameterValue: {{$parameterName}}ParameterValue,
AllowedValues: {{$parameterName}}ValidValues,
}
}
}
{{end}}
{{end}}
{{if eq $property.Type "object"}}
if v.{{$property.ID | camelCase}} != nil {
if err := v.{{$property.ID | camelCase}}.Validate(); err != nil {
return err
}
}
{{if $property.IsRequired }}
if v.{{$property.ID | camelCase}} == nil {
return errors.ParameterRequiredError{
ParameterName: "{{$property.ID | camelCase}}",
ParentName: "{{$customizedType.ID | camelCase}}",
}
}
{{end}}
{{end}}
{{if eq $property.Type "array"}}
{{if $property.IsRequired}}
if len(v.{{$property.ID | camelCase}}) == 0 {
return errors.ParameterRequiredError{
ParameterName: "{{$property.ID | camelCase}}",
ParentName: "{{$customizedType.ID | camelCase}}",
}
}
{{end}}
{{$isNotString := ne $property.ExtraType "string"}}
{{$isNotInteger := ne $property.ExtraType "integer"}}
{{$isNotTimestamp := ne $property.ExtraType "timestamp"}}
{{if and $isNotString $isNotInteger $isNotTimestamp}}
if len(v.{{$property.ID | camelCase}}) > 0 {
for _, property := range v.{{$property.ID | camelCase}} {
if err := property.Validate(); err != nil {
return err
}
}
}
{{end}}
{{end}}
{{end}}
{{end}}
{{end}}