Skip to content

Commit

Permalink
Merge pull request #86 from volcengine/bugfix/json_trans
Browse files Browse the repository at this point in the history
Bugfix/json trans
  • Loading branch information
xuyaming0800 authored Apr 26, 2023
2 parents 7766c09 + f6871ac commit f34f22f
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 28 deletions.
149 changes: 125 additions & 24 deletions common/common_volcengine_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,123 @@ func (c *SdkCall) InitWriteCall(resourceData *schema.ResourceData, resource *sch
return err
}

func SortAndStartTransJson(source map[string]interface{}) map[string]interface{} {
func volcengineSort(source []string) (result []string) {
mapping := make(map[string]int)
for _, k := range source {
if strings.Contains(k, ".") {
keys := strings.Split(k, ".")
var key string
for _, v1 := range keys {
if n, err := strconv.Atoi(v1); err != nil {
key = key + v1 + "."
} else {
if _n, ok := mapping[key]; !ok {
mapping[key] = len(strconv.Itoa(n))
} else {
if len(strconv.Itoa(n)) > _n {
mapping[key] = len(strconv.Itoa(n))
}
}
}
}

}
}
for _, k := range source {
if strings.Contains(k, ".") {
keys := strings.Split(k, ".")
var (
key string
newKey string
)
for _, v1 := range keys {
if n, err := strconv.Atoi(v1); err != nil {
key = key + v1 + "."
newKey = newKey + v1 + "."
} else {
if len(strconv.Itoa(n)) < mapping[key] {
var zero string
for i := 0; i < mapping[key]-len(strconv.Itoa(n)); i++ {
zero = zero + "0"
}
newKey = newKey + zero + strconv.Itoa(n) + "."
} else {
newKey = newKey + strconv.Itoa(n) + "."
}
}
}
result = append(result, newKey[0:len(newKey)-1])
} else {
result = append(result, k)
}
}

sort.Strings(result)

//去除补位
for i, k := range result {
if strings.Contains(k, ".") {
keys := strings.Split(k, ".")
var (
newKey string
)
for _, v1 := range keys {
if strings.HasPrefix(v1, "0") {
b := []byte(v1)
var (
newB []byte
hasNoZero bool
)

for _, v2 := range b {
if v2 != '0' && !hasNoZero {
hasNoZero = true
newB = append(newB, v2)
} else if hasNoZero {
newB = append(newB, v2)
}
}
newKey = newKey + string(newB) + "."

} else {
newKey = newKey + v1 + "."
}
}
result[i] = newKey[0 : len(newKey)-1]
}
}

return result
}

func SortAndStartTransJson(source map[string]interface{}) (map[string]interface{}, error) {
target := make(map[string]interface{})
var a []string
for k := range source {
a = append(a, k)
}
sort.Strings(a)

a = volcengineSort(a)

for _, k := range a {
k1, v1 := transToJson(k, source[k], "", &target)
k1, v1, err := transToJson(k, source[k], "", &target)
if err != nil {
return target, err
}
target[k1] = v1
}
return target
return target, nil
}

func transToJson(key string, value interface{}, chain string, top *map[string]interface{}) (string, interface{}) {
func transToJson(key string, value interface{}, chain string, top *map[string]interface{}) (string, interface{}, error) {
var (
index int
err error
k string
v interface{}
v1 []interface{}
v2 map[string]interface{}
)
if strings.Contains(key, ".") {
keys := strings.Split(key, ".")
Expand All @@ -109,49 +207,48 @@ func transToJson(key string, value interface{}, chain string, top *map[string]in
chain = chain + "." + strconv.Itoa(index-1)
}
}
k, v := transToJson(nextKey, value, chain, top)
k, v, err = transToJson(nextKey, value, chain, top)
if err != nil {
return k, v, err
}
index, err = strconv.Atoi(k)
if err == nil {
return keys[0], getAndSetSlice(chain, index-1, v, top)
v1, err = getAndSetSlice(chain, index-1, v, top)
return keys[0], v1, err
} else {
return keys[0], getAndSetMap(chain, k, v, top)
v2, err = getAndSetMap(chain, k, v, top)
return keys[0], v2, err
}
} else {
return key, value
return key, value, nil
}
}

func getAndSetSlice(pattern string, index int, value interface{}, top *map[string]interface{}) []interface{} {
func getAndSetSlice(pattern string, index int, value interface{}, top *map[string]interface{}) ([]interface{}, error) {
exist, _ := ObtainSdkValue(pattern, *top)
if exist != nil {
exist1, _ := ObtainSdkValue(pattern+"."+strconv.Itoa(index), *top)
if exist1 == nil {
if len(exist.([]interface{})) < index+1 {
n := make([]interface{}, index+1)
n[index] = value
for i, v := range exist.([]interface{}) {
n[i] = v
}
return n
} else {
exist.([]interface{})[index] = value
if index > len(exist.([]interface{})) {
return exist.([]interface{}), fmt.Errorf("%s index is not serial", pattern+"."+strconv.Itoa(index+1))
}
return append(exist.([]interface{}), value), nil
}
return exist.([]interface{})
return exist.([]interface{}), nil
}
return []interface{}{value}
return []interface{}{value}, nil
}

func getAndSetMap(pattern string, key string, value interface{}, top *map[string]interface{}) map[string]interface{} {
func getAndSetMap(pattern string, key string, value interface{}, top *map[string]interface{}) (map[string]interface{}, error) {
exist, _ := ObtainSdkValue(pattern, *top)
if exist != nil {
next := exist.(map[string]interface{})
next[key] = value
return next
return next, nil
}
return map[string]interface{}{
key: value,
}
}, nil
}

func (c *SdkCall) InitReadCall(resourceData *schema.ResourceData, resource *schema.Resource) (err error) {
Expand Down Expand Up @@ -199,7 +296,11 @@ func CallProcess(calls []SdkCall, d *schema.ResourceData, client *SdkClient, ser
case ContentTypeDefault:
break
case ContentTypeJson:
jsonParam := SortAndStartTransJson(*fn.SdkParam)
var jsonParam map[string]interface{}
jsonParam, err = SortAndStartTransJson(*fn.SdkParam)
if err != nil {
return err
}
fn.SdkParam = &jsonParam
break
}
Expand Down
5 changes: 4 additions & 1 deletion common/common_volcengine_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ func (d *Dispatcher) Data(resourceService ResourceService, resourceDate *schema.
return err
}
if info.ContentType == ContentTypeJson {
condition = SortAndStartTransJson(condition)
condition, err = SortAndStartTransJson(condition)
if err != nil {
return err
}
}
switch info.ServiceCategory {
case ServiceTos:
Expand Down
7 changes: 5 additions & 2 deletions common/common_volcengine_tos_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,11 @@ func BeforeTosPutAcl(d *schema.ResourceData, call SdkCall, data *map[string]inte
if err != nil {
return false, err
}

sourceAclParam := SortAndStartTransJson((*call.SdkParam)[TosParam].(map[string]interface{}))
var sourceAclParam map[string]interface{}
sourceAclParam, err = SortAndStartTransJson((*call.SdkParam)[TosParam].(map[string]interface{}))
if err != nil {
return false, err
}
ownerId, _ := ObtainSdkValue("Owner.ID", (*data)[TosResponse])

grants, _ := ObtainSdkValue("Grants", sourceAclParam)
Expand Down
2 changes: 1 addition & 1 deletion common/common_volcengine_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package common

const (
TerraformProviderName = "terraform-provider-volcengine"
TerraformProviderVersion = "0.0.70"
TerraformProviderVersion = "0.0.71"
)

0 comments on commit f34f22f

Please sign in to comment.