Skip to content

Commit

Permalink
fix url encode
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Apr 7, 2024
1 parent c76fd8a commit ef93f2a
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 17 deletions.
35 changes: 27 additions & 8 deletions rest/internal/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package internal

type StringSlicePairs []*StringSlicePair
import (
"fmt"
"strings"
)

type StringSlicePairs []StringSlicePair

// String implements fmt.Stringer interface
func (ssp StringSlicePairs) String() string {
results := make([]string, len(ssp))
for _, item := range ssp {
results = append(results, item.String())
}
return strings.Join(results, "|")
}

func (ssp *StringSlicePairs) Add(keys []string, values []string) {
index := ssp.FindIndex(keys)
Expand Down Expand Up @@ -36,15 +50,14 @@ func (ssp StringSlicePairs) find(keys []string) (*StringSlicePair, int) {
continue
}
if len(keys) == 0 {
return item, i
return &item, i
}
isEqual := false
for j, value := range item.keys {
isEqual = value == keys[j]
}

if isEqual {
return item, i
if !isEqual {
return nil, -1
}
}
}
return nil, -1
Expand All @@ -55,13 +68,19 @@ type StringSlicePair struct {
values []string
}

func NewStringSlicePair(keys []string, values []string) *StringSlicePair {
return &StringSlicePair{
// NewStringSlicePair creates a string slice pair
func NewStringSlicePair(keys []string, values []string) StringSlicePair {
return StringSlicePair{
keys: keys,
values: values,
}
}

// String implements fmt.Stringer interface
func (ssp StringSlicePair) String() string {
return fmt.Sprintf("%s=%s", strings.Join(ssp.keys, ""), strings.Join(ssp.values, ","))
}

func (ssp StringSlicePair) Keys() []string {
return ssp.keys
}
Expand Down
19 changes: 10 additions & 9 deletions rest/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ func (c *RESTConnector) encodeParameterValues(typeSchema *rest.TypeSchema, value
if err != nil {
return nil, err
}

for _, pair := range output {
results = append(results, internal.NewStringSlicePair(append([]string{k}, pair.Keys()...), pair.Values()))
results.Add(append([]string{k}, pair.Keys()...), pair.Values())
}
}

Expand All @@ -161,7 +162,7 @@ func (c *RESTConnector) encodeParameterValues(typeSchema *rest.TypeSchema, value
}

for _, output := range outputs {
results.Add(append([]string{""}, output.Keys()...), output.Values())
results.Add(append([]string{fmt.Sprint(i)}, output.Keys()...), output.Values())
}
}
return results, nil
Expand Down Expand Up @@ -191,13 +192,13 @@ func (c *RESTConnector) encodeParameterValues(typeSchema *rest.TypeSchema, value
return nil, fmt.Errorf("%s: invalid enum value '%s'", strings.Join(fieldPaths, ""), valueStr)
}

return []*internal.StringSlicePair{internal.NewStringSlicePair([]string{}, []string{valueStr})}, nil
return []internal.StringSlicePair{internal.NewStringSlicePair([]string{}, []string{valueStr})}, nil
case *schema.TypeRepresentationInt8, *schema.TypeRepresentationInt16, *schema.TypeRepresentationInt32, *schema.TypeRepresentationInt64, *schema.TypeRepresentationBigDecimal:
return encodeParameterInt(value, fieldPaths)
case *schema.TypeRepresentationFloat32, *schema.TypeRepresentationFloat64:
return encodeParameterFloat(value, fieldPaths)
default:
return []*internal.StringSlicePair{
return []internal.StringSlicePair{
internal.NewStringSlicePair([]string{}, []string{fmt.Sprint(value)}),
}, nil
}
Expand All @@ -209,7 +210,7 @@ func (c *RESTConnector) encodeParameterValues(typeSchema *rest.TypeSchema, value
return nil, fmt.Errorf("%s: %s", strings.Join(fieldPaths, ""), err)
}
values := []string{strings.Trim(string(b), `"`)}
return []*internal.StringSlicePair{internal.NewStringSlicePair([]string{}, values)}, nil
return []internal.StringSlicePair{internal.NewStringSlicePair([]string{}, values)}, nil
}
}

Expand Down Expand Up @@ -294,7 +295,7 @@ func encodeParameterBool(value any, fieldPaths []string) (internal.StringSlicePa
return nil, fmt.Errorf("%s: %s", strings.Join(fieldPaths, ""), err)
}

return []*internal.StringSlicePair{
return []internal.StringSlicePair{
internal.NewStringSlicePair([]string{}, []string{strconv.FormatBool(result)}),
}, nil
}
Expand All @@ -304,21 +305,21 @@ func encodeParameterString(value any, fieldPaths []string) (internal.StringSlice
if err != nil {
return nil, fmt.Errorf("%s: %s", strings.Join(fieldPaths, ""), err)
}
return []*internal.StringSlicePair{internal.NewStringSlicePair([]string{}, []string{result})}, nil
return []internal.StringSlicePair{internal.NewStringSlicePair([]string{}, []string{result})}, nil
}

func encodeParameterInt(value any, fieldPaths []string) (internal.StringSlicePairs, error) {
intValue, err := sdkUtils.DecodeInt[int64](value)
if err != nil {
return nil, fmt.Errorf("%s: %s", strings.Join(fieldPaths, ""), err)
}
return []*internal.StringSlicePair{internal.NewStringSlicePair([]string{}, []string{strconv.FormatInt(intValue, 10)})}, nil
return []internal.StringSlicePair{internal.NewStringSlicePair([]string{}, []string{strconv.FormatInt(intValue, 10)})}, nil
}

func encodeParameterFloat(value any, fieldPaths []string) (internal.StringSlicePairs, error) {
floatValue, err := sdkUtils.DecodeFloat[float64](value)
if err != nil {
return nil, fmt.Errorf("%s: %s", strings.Join(fieldPaths, ""), err)
}
return []*internal.StringSlicePair{internal.NewStringSlicePair([]string{}, []string{fmt.Sprintf("%f", floatValue)})}, nil
return []internal.StringSlicePair{internal.NewStringSlicePair([]string{}, []string{fmt.Sprintf("%f", floatValue)})}, nil
}
Loading

0 comments on commit ef93f2a

Please sign in to comment.