Skip to content

Commit

Permalink
Merge pull request #64 from nifcloud/feature/add_custom_deserializer_…
Browse files Browse the repository at this point in the history
…for_elb

Add custom deserializers for ELB
  • Loading branch information
tunakyonn authored Dec 18, 2023
2 parents 588e174 + 480167e commit 973db7e
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 5 deletions.
179 changes: 179 additions & 0 deletions internal/deserializers/deserializers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package deserializers

import (
"fmt"
"strconv"
"strings"

smithyxml "github.com/aws/smithy-go/encoding/xml"
"github.com/aws/smithy-go/ptr"
"github.com/nifcloud/nifcloud-sdk-go/service/computing/types"
)

// DeserializeDocumentSessionStickinessPolicy is custom deserializer to parse empty 'ExpirationPeriod'
func DeserializeDocumentSessionStickinessPolicy(
v **types.SessionStickinessPolicy,
decoder smithyxml.NodeDecoder,
) error {
if v == nil {
return fmt.Errorf("unexpected nil of type %T", v)
}
var sv *types.SessionStickinessPolicy
if *v == nil {
sv = &types.SessionStickinessPolicy{}
} else {
sv = *v
}

for {
t, done, err := decoder.Token()
if err != nil {
return err
}
if done {
break
}
originalDecoder := decoder
decoder = smithyxml.WrapNodeDecoder(originalDecoder.Decoder, t)
switch {
case strings.EqualFold("Enabled", t.Name.Local):
val, err := decoder.Value()
if err != nil {
return err
}
if val == nil {
break
}
{
xtv, err := strconv.ParseBool(string(val))
if err != nil {
return fmt.Errorf("expected Boolean to be of type *bool, got %T instead", val)
}
sv.Enabled = ptr.Bool(xtv)
}

case strings.EqualFold("ExpirationPeriod", t.Name.Local):
val, err := decoder.Value()
if err != nil {
return err
}
if val == nil {
break
}
{
xtv := string(val)
if xtv != "" {
i64, err := strconv.ParseInt(xtv, 10, 64)
if err != nil {
return err
}
sv.ExpirationPeriod = ptr.Int32(int32(i64))
}
}

default:
// Do nothing and ignore the unexpected tag element
err = decoder.Decoder.Skip()
if err != nil {
return err
}

}
decoder = originalDecoder
}
*v = sv
return nil
}

// DeserializeDocumentSessionStickinessPolicyOfNiftyDescribeElasticLoadBalancers is custom deserializer
// to parse empty 'ExpirationPeriod'
func DeserializeDocumentSessionStickinessPolicyOfNiftyDescribeElasticLoadBalancers(
v **types.SessionStickinessPolicyOfNiftyDescribeElasticLoadBalancers,
decoder smithyxml.NodeDecoder,
) error {
if v == nil {
return fmt.Errorf("unexpected nil of type %T", v)
}
var sv *types.SessionStickinessPolicyOfNiftyDescribeElasticLoadBalancers
if *v == nil {
sv = &types.SessionStickinessPolicyOfNiftyDescribeElasticLoadBalancers{}
} else {
sv = *v
}

for {
t, done, err := decoder.Token()
if err != nil {
return err
}
if done {
break
}
originalDecoder := decoder
decoder = smithyxml.WrapNodeDecoder(originalDecoder.Decoder, t)
switch {
case strings.EqualFold("Enabled", t.Name.Local):
val, err := decoder.Value()
if err != nil {
return err
}
if val == nil {
break
}
{
xtv, err := strconv.ParseBool(string(val))
if err != nil {
return fmt.Errorf("expected Boolean to be of type *bool, got %T instead", val)
}
sv.Enabled = ptr.Bool(xtv)
}

case strings.EqualFold("ExpirationPeriod", t.Name.Local):
val, err := decoder.Value()
if err != nil {
return err
}
if val == nil {
break
}
{
xtv := string(val)
if xtv != "" {
i64, err := strconv.ParseInt(xtv, 10, 64)
if err != nil {
return err
}
sv.ExpirationPeriod = ptr.Int32(int32(i64))
}
}

case strings.EqualFold("Method", t.Name.Local):
val, err := decoder.Value()
if err != nil {
return err
}
if val == nil {
break
}
{
xtv := string(val)
i64, err := strconv.ParseInt(xtv, 10, 64)
if err != nil {
return err
}
sv.Method = ptr.Int32(int32(i64))
}

default:
// Do nothing and ignore the unexpected tag element
err = decoder.Decoder.Skip()
if err != nil {
return err
}

}
decoder = originalDecoder
}
*v = sv
return nil
}
26 changes: 23 additions & 3 deletions private/model/cli/mod-api/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build codegen
// +build codegen

package main
Expand Down Expand Up @@ -278,16 +279,35 @@ func rewriteSerializersFile(path string) error {
}

func rewriteDeserializersFile(path string) error {
imports := []map[string]string{
{"github.com/aws/aws-sdk-go-v2/service/internal/s3shared": "github.com/nifcloud/nifcloud-sdk-go/service/internal/s3shared"},
}
imports := []map[string]string{}
replaces := []map[string]string{}

// remove Operation suffix
replaces = append(replaces, map[string]string{
"OperationResult": "Result",
})

serviceName := filepath.Base(filepath.Dir(path))
if serviceName == "computing" {
imports = append(imports, map[string]string{
"": "github.com/nifcloud/nifcloud-sdk-go/internal/deserializers",
})

// to use internal/deserializers
replaces = append(
replaces,
map[string]string{
"awsEc2query_deserializeDocumentSessionStickinessPolicy\\(&sv\\.": "deserializers.DeserializeDocumentSessionStickinessPolicy(&sv.",
"awsEc2query_deserializeDocumentSessionStickinessPolicyOfNiftyDescribeElasticLoadBalancers\\(&sv\\.": "deserializers.DeserializeDocumentSessionStickinessPolicyOfNiftyDescribeElasticLoadBalancers(&sv.",
})
}

if serviceName == "storage" {
imports = append(imports, map[string]string{
"github.com/aws/aws-sdk-go-v2/service/internal/s3shared": "github.com/nifcloud/nifcloud-sdk-go/service/internal/s3shared",
})
}

return rewrite(path, imports, replaces)
}

Expand Down
5 changes: 3 additions & 2 deletions service/computing/deserializers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 973db7e

Please sign in to comment.