Skip to content

Commit

Permalink
swagger support enum in query. fix deep nested type handling. refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
t-yuki committed Jan 17, 2017
1 parent 56af20c commit 28132c2
Show file tree
Hide file tree
Showing 11 changed files with 739 additions and 182 deletions.
110 changes: 110 additions & 0 deletions examples/clients/abe/ABitOfEverythingServiceApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"github.com/dghubble/sling"
"time"
)

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

return *successPayload, err
}
/**
*
*
* @param uuid
* @param singleNestedName name is nested field.
* @param singleNestedAmount
* @param singleNestedOk - FALSE: FALSE is false.\n - TRUE: TRUE is true.
* @param floatValue
* @param doubleValue
* @param int64Value
* @param uint64Value
* @param int32Value
* @param fixed64Value
* @param fixed32Value
* @param boolValue
* @param stringValue
* @param uint32Value TODO(yugui) add bytes_value.
* @param enumValue - ZERO: ZERO means 0\n - ONE: ONE means 1
* @param sfixed32Value
* @param sfixed64Value
* @param sint32Value
* @param sint64Value
* @param repeatedStringValue
* @param oneofString
* @param nonConventionalNameValue
* @param timestampValue
* @param repeatedEnumValue repeated enum value. it is comma-separated in query.\n\n - ZERO: ZERO means 0\n - ONE: ONE means 1
* @return ProtobufEmpty
*/
//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) {
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) {

_sling := sling.New().Get(a.basePath)

// create path and map variables
path := "/v1/example/a_bit_of_everything/query/{uuid}"
path = strings.Replace(path, "{" + "uuid" + "}", fmt.Sprintf("%v", uuid), -1)

_sling = _sling.Path(path)

type QueryParams struct {
singleNestedName string `url:"single_nested.name,omitempty"`
singleNestedAmount int64 `url:"single_nested.amount,omitempty"`
singleNestedOk string `url:"single_nested.ok,omitempty"`
floatValue float32 `url:"float_value,omitempty"`
doubleValue float64 `url:"double_value,omitempty"`
int64Value string `url:"int64_value,omitempty"`
uint64Value string `url:"uint64_value,omitempty"`
int32Value int32 `url:"int32_value,omitempty"`
fixed64Value string `url:"fixed64_value,omitempty"`
fixed32Value int64 `url:"fixed32_value,omitempty"`
boolValue bool `url:"bool_value,omitempty"`
stringValue string `url:"string_value,omitempty"`
uint32Value int64 `url:"uint32_value,omitempty"`
enumValue string `url:"enum_value,omitempty"`
sfixed32Value int32 `url:"sfixed32_value,omitempty"`
sfixed64Value string `url:"sfixed64_value,omitempty"`
sint32Value int32 `url:"sint32_value,omitempty"`
sint64Value string `url:"sint64_value,omitempty"`
repeatedStringValue []string `url:"repeated_string_value,omitempty"`
oneofString string `url:"oneof_string,omitempty"`
nonConventionalNameValue string `url:"nonConventionalNameValue,omitempty"`
timestampValue time.Time `url:"timestamp_value,omitempty"`
repeatedEnumValue []string `url:"repeated_enum_value,omitempty"`

}
_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 })
// accept header
accepts := []string { "application/json" }
for key := range accepts {
_sling = _sling.Set("Accept", accepts[key])
break // only use the first Accept
}


var successPayload = new(ProtobufEmpty)

// We use this map (below) so that any arbitrary error JSON can be handled.
// FIXME: This is in the absence of this Go generator honoring the non-2xx
// response (error) models, which needs to be implemented at some point.
var failurePayload map[string]interface{}

httpResponse, err := _sling.Receive(successPayload, &failurePayload)

if err == nil {
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
if failurePayload != nil {
// If the failurePayload is present, there likely was some kind of non-2xx status
// returned (and a JSON payload error present)
var str []byte
str, err = json.Marshal(failurePayload)
if err == nil { // For safety, check for an error marshalling... probably superfluous
// This will return the JSON error body as a string
err = errors.New(string(str))
}
} else {
// So, there was no network-type error, and nothing in the failure payload,
// but we should still check the status code
if httpResponse == nil {
// This should never happen...
err = errors.New("No HTTP Response received.")
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
}
}
}

return *successPayload, err
}
/**
*
*
Expand Down
1 change: 1 addition & 0 deletions examples/clients/abe/ExamplepbABitOfEverything.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ type ExamplepbABitOfEverything struct {
MappedNestedValue map[string]ABitOfEverythingNested `json:"mapped_nested_value,omitempty"`
NonConventionalNameValue string `json:"nonConventionalNameValue,omitempty"`
TimestampValue time.Time `json:"timestamp_value,omitempty"`
RepeatedEnumValue []ExamplepbNumericEnum `json:"repeated_enum_value,omitempty"`

}
Loading

0 comments on commit 28132c2

Please sign in to comment.