Skip to content

Commit

Permalink
Merge pull request #3826 from hashicorp/bugfix/resp-model-unmarshaling
Browse files Browse the repository at this point in the history
Improve unmarshalling for binary payloads
  • Loading branch information
manicminer authored Feb 23, 2024
2 parents 6a68c0b + 857c82e commit 54ec352
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
4 changes: 4 additions & 0 deletions tools/data-api/internal/commands/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (c ServeCommand) Run(args []string) int {
serviceNames = pointer.To(strings.Split(serviceNamesRaw, ","))
}

if logLevel := strings.TrimSpace(os.Getenv("LOG_LEVEL")); logLevel != "" {
c.Log.SetLevel(hclog.LevelFromString(logLevel))
}

var port int
if portVar != 0 {
port = portVar
Expand Down
22 changes: 19 additions & 3 deletions tools/generator-go-sdk/internal/generator/templater_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,11 +618,27 @@ func (c methodsPandoraTemplater) unmarshalerTemplate(data ServiceGeneratorData)
result.Model = &model
`, discriminatedTypeParentName)
} else {
output = fmt.Sprintf(`
if err = resp.Unmarshal(&result.Model); err != nil {
responseModelType, err := helpers.GolangTypeForSDKObjectDefinition(*c.operation.ResponseObject, nil)
if err != nil {
return nil, fmt.Errorf("determing golang type name for response object: %+v", err)
}
if responseModelType != nil {
if c.operation.FieldContainingPaginationDetails != nil {
output = fmt.Sprintf(`
var model []%s`, *responseModelType)
} else {
output = fmt.Sprintf(`
var model %s`, *responseModelType)
}
output = fmt.Sprintf(`%s
result.Model = &model`, output)
}
output = fmt.Sprintf(`%s
if err = resp.Unmarshal(result.Model); err != nil {
return
}
`)
`, output)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ func (c pandaClient) Get(ctx context.Context ) (result GetOperationResponse, err
return
}
if err = resp.Unmarshal(&result.Model); err != nil {
var model PandaPop
result.Model = &model
if err = resp.Unmarshal(result.Model); err != nil {
return
}
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func (c pandaClient) Get(ctx context.Context , id PandaPop) (result GetOperation
return
}
if err = resp.Unmarshal(&result.Model); err != nil {
var model string
result.Model = &model
if err = resp.Unmarshal(result.Model); err != nil {
return
}
Expand Down Expand Up @@ -104,7 +106,7 @@ func TestTemplateMethodsGetAsTextPowerShell(t *testing.T) {
Method: "GET",
ResourceIDName: stringPointer("PandaPop"),
ResponseObject: &models.SDKObjectDefinition{
Type: models.StringSDKObjectDefinitionType,
Type: models.RawFileSDKObjectDefinitionType,
},
},
operationName: "Get",
Expand All @@ -118,7 +120,7 @@ func TestTemplateMethodsGetAsTextPowerShell(t *testing.T) {
type GetOperationResponse struct {
HttpResponse *http.Response
OData *odata.OData
Model *string
Model *[]byte
}
// Get ...
Expand Down Expand Up @@ -147,7 +149,9 @@ func (c pandaClient) Get(ctx context.Context , id PandaPop) (result GetOperation
return
}
if err = resp.Unmarshal(&result.Model); err != nil {
var model []byte
result.Model = &model
if err = resp.Unmarshal(result.Model); err != nil {
return
}
Expand Down

0 comments on commit 54ec352

Please sign in to comment.