Skip to content

Commit 28132c2

Browse files
committed
swagger support enum in query. fix deep nested type handling. refactor. #199
1 parent 56af20c commit 28132c2

File tree

11 files changed

+739
-182
lines changed

11 files changed

+739
-182
lines changed

examples/clients/abe/ABitOfEverythingServiceApi.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"github.com/dghubble/sling"
9+
"time"
910
)
1011

1112
type ABitOfEverythingServiceApi struct {
@@ -474,6 +475,115 @@ func (a ABitOfEverythingServiceApi) Echo_2 (body string) (SubStringMessage, erro
474475

475476
return *successPayload, err
476477
}
478+
/**
479+
*
480+
*
481+
* @param uuid
482+
* @param singleNestedName name is nested field.
483+
* @param singleNestedAmount
484+
* @param singleNestedOk - FALSE: FALSE is false.\n - TRUE: TRUE is true.
485+
* @param floatValue
486+
* @param doubleValue
487+
* @param int64Value
488+
* @param uint64Value
489+
* @param int32Value
490+
* @param fixed64Value
491+
* @param fixed32Value
492+
* @param boolValue
493+
* @param stringValue
494+
* @param uint32Value TODO(yugui) add bytes_value.
495+
* @param enumValue - ZERO: ZERO means 0\n - ONE: ONE means 1
496+
* @param sfixed32Value
497+
* @param sfixed64Value
498+
* @param sint32Value
499+
* @param sint64Value
500+
* @param repeatedStringValue
501+
* @param oneofString
502+
* @param nonConventionalNameValue
503+
* @param timestampValue
504+
* @param repeatedEnumValue repeated enum value. it is comma-separated in query.\n\n - ZERO: ZERO means 0\n - ONE: ONE means 1
505+
* @return ProtobufEmpty
506+
*/
507+
//func (a ABitOfEverythingServiceApi) GetQuery (uuid string, singleNestedName string, singleNestedAmount int64, singleNestedOk string, floatValue float32, doubleValue float64, int64Value string, uint64Value string, int32Value int32, fixed64Value string, fixed32Value int64, boolValue bool, stringValue string, uint32Value int64, enumValue string, sfixed32Value int32, sfixed64Value string, sint32Value int32, sint64Value string, repeatedStringValue []string, oneofString string, nonConventionalNameValue string, timestampValue time.Time, repeatedEnumValue []string) (ProtobufEmpty, error) {
508+
func (a ABitOfEverythingServiceApi) GetQuery (uuid string, singleNestedName string, singleNestedAmount int64, singleNestedOk string, floatValue float32, doubleValue float64, int64Value string, uint64Value string, int32Value int32, fixed64Value string, fixed32Value int64, boolValue bool, stringValue string, uint32Value int64, enumValue string, sfixed32Value int32, sfixed64Value string, sint32Value int32, sint64Value string, repeatedStringValue []string, oneofString string, nonConventionalNameValue string, timestampValue time.Time, repeatedEnumValue []string) (ProtobufEmpty, error) {
509+
510+
_sling := sling.New().Get(a.basePath)
511+
512+
// create path and map variables
513+
path := "/v1/example/a_bit_of_everything/query/{uuid}"
514+
path = strings.Replace(path, "{" + "uuid" + "}", fmt.Sprintf("%v", uuid), -1)
515+
516+
_sling = _sling.Path(path)
517+
518+
type QueryParams struct {
519+
singleNestedName string `url:"single_nested.name,omitempty"`
520+
singleNestedAmount int64 `url:"single_nested.amount,omitempty"`
521+
singleNestedOk string `url:"single_nested.ok,omitempty"`
522+
floatValue float32 `url:"float_value,omitempty"`
523+
doubleValue float64 `url:"double_value,omitempty"`
524+
int64Value string `url:"int64_value,omitempty"`
525+
uint64Value string `url:"uint64_value,omitempty"`
526+
int32Value int32 `url:"int32_value,omitempty"`
527+
fixed64Value string `url:"fixed64_value,omitempty"`
528+
fixed32Value int64 `url:"fixed32_value,omitempty"`
529+
boolValue bool `url:"bool_value,omitempty"`
530+
stringValue string `url:"string_value,omitempty"`
531+
uint32Value int64 `url:"uint32_value,omitempty"`
532+
enumValue string `url:"enum_value,omitempty"`
533+
sfixed32Value int32 `url:"sfixed32_value,omitempty"`
534+
sfixed64Value string `url:"sfixed64_value,omitempty"`
535+
sint32Value int32 `url:"sint32_value,omitempty"`
536+
sint64Value string `url:"sint64_value,omitempty"`
537+
repeatedStringValue []string `url:"repeated_string_value,omitempty"`
538+
oneofString string `url:"oneof_string,omitempty"`
539+
nonConventionalNameValue string `url:"nonConventionalNameValue,omitempty"`
540+
timestampValue time.Time `url:"timestamp_value,omitempty"`
541+
repeatedEnumValue []string `url:"repeated_enum_value,omitempty"`
542+
543+
}
544+
_sling = _sling.QueryStruct(&QueryParams{ singleNestedName: singleNestedName,singleNestedAmount: singleNestedAmount,singleNestedOk: singleNestedOk,floatValue: floatValue,doubleValue: doubleValue,int64Value: int64Value,uint64Value: uint64Value,int32Value: int32Value,fixed64Value: fixed64Value,fixed32Value: fixed32Value,boolValue: boolValue,stringValue: stringValue,uint32Value: uint32Value,enumValue: enumValue,sfixed32Value: sfixed32Value,sfixed64Value: sfixed64Value,sint32Value: sint32Value,sint64Value: sint64Value,repeatedStringValue: repeatedStringValue,oneofString: oneofString,nonConventionalNameValue: nonConventionalNameValue,timestampValue: timestampValue,repeatedEnumValue: repeatedEnumValue })
545+
// accept header
546+
accepts := []string { "application/json" }
547+
for key := range accepts {
548+
_sling = _sling.Set("Accept", accepts[key])
549+
break // only use the first Accept
550+
}
551+
552+
553+
var successPayload = new(ProtobufEmpty)
554+
555+
// We use this map (below) so that any arbitrary error JSON can be handled.
556+
// FIXME: This is in the absence of this Go generator honoring the non-2xx
557+
// response (error) models, which needs to be implemented at some point.
558+
var failurePayload map[string]interface{}
559+
560+
httpResponse, err := _sling.Receive(successPayload, &failurePayload)
561+
562+
if err == nil {
563+
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
564+
if failurePayload != nil {
565+
// If the failurePayload is present, there likely was some kind of non-2xx status
566+
// returned (and a JSON payload error present)
567+
var str []byte
568+
str, err = json.Marshal(failurePayload)
569+
if err == nil { // For safety, check for an error marshalling... probably superfluous
570+
// This will return the JSON error body as a string
571+
err = errors.New(string(str))
572+
}
573+
} else {
574+
// So, there was no network-type error, and nothing in the failure payload,
575+
// but we should still check the status code
576+
if httpResponse == nil {
577+
// This should never happen...
578+
err = errors.New("No HTTP Response received.")
579+
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
580+
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
581+
}
582+
}
583+
}
584+
585+
return *successPayload, err
586+
}
477587
/**
478588
*
479589
*

examples/clients/abe/ExamplepbABitOfEverything.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ type ExamplepbABitOfEverything struct {
3131
MappedNestedValue map[string]ABitOfEverythingNested `json:"mapped_nested_value,omitempty"`
3232
NonConventionalNameValue string `json:"nonConventionalNameValue,omitempty"`
3333
TimestampValue time.Time `json:"timestamp_value,omitempty"`
34+
RepeatedEnumValue []ExamplepbNumericEnum `json:"repeated_enum_value,omitempty"`
3435

3536
}

0 commit comments

Comments
 (0)