Skip to content

Commit

Permalink
Merge pull request #10 from jmorissette/fix-modifying-variation-on-fl…
Browse files Browse the repository at this point in the history
…ag-causes-unexpected-EOF

Fix indices in for loop to prevent out of bound exception
  • Loading branch information
jmorissette authored Mar 31, 2020
2 parents 8bf0340 + 8a8d073 commit 7dad605
Showing 1 changed file with 64 additions and 41 deletions.
105 changes: 64 additions & 41 deletions launchdarkly/resource_feature_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,66 +513,89 @@ func applyChangesToVariations(resourceData *schema.ResourceData, client Client)

//Remove variations
if newNumberOfVariation < actualNumberOfVariation {
var payloadValue []interface{} = make([]interface{}, actualNumberOfVariation - newNumberOfVariation)

for i := actualNumberOfVariation - 1; i >= newNumberOfVariation; i-- {
var deletePayloadValue []interface{} = make([]interface{}, actualNumberOfVariation - newNumberOfVariation)
for i := 0; i < len(deletePayloadValue); i++ {
removeValue := map[string]interface{}{
"op": "remove",
"path": fmt.Sprintf("/variations/%d", i),
"path": fmt.Sprintf("/variations/%d", actualNumberOfVariation - 1),
}
payloadValue[i] = removeValue
deletePayloadValue[i] = removeValue
actualNumberOfVariation--
}

_, err = client.Patch(getFlagUrl(project, key), payloadValue, []int{200}, NUMBER_OF_RETRY)
_, err = client.Patch(getFlagUrl(project, key), deletePayloadValue, []int{200}, NUMBER_OF_RETRY)
if err != nil {
return err
}
}

//Update values off existing variations
var payloadValue []interface{} = make([]interface{}, 3*actualNumberOfVariation)
for i := 0; i <= actualNumberOfVariation-1; i++ {
replaceValue := map[string]interface{}{
"op": "replace",
"path": fmt.Sprintf("/variations/%d/value", i),
"value": transformedVariations[i].Value,
}
replaceName := map[string]interface{}{
"op": "replace",
"path": fmt.Sprintf("/variations/%d/name", i),
"value": transformedVariations[i].Name,
//Update values off existing variations that were not delete
var updatePayloadValue []interface{} = make([]interface{}, 3*newNumberOfVariation)
for i := 0; i < newNumberOfVariation; i++ {
replaceValue := map[string]interface{}{
"op": "replace",
"path": fmt.Sprintf("/variations/%d/value", i),
"value": transformedVariations[i].Value,
}
replaceName := map[string]interface{}{
"op": "replace",
"path": fmt.Sprintf("/variations/%d/name", i),
"value": transformedVariations[i].Name,
}
replaceDescription := map[string]interface{}{
"op": "replace",
"path": fmt.Sprintf("/variations/%d/description", i),
"value": transformedVariations[i].Description,
}
updatePayloadValue[i*3] = replaceValue
updatePayloadValue[(i*3)+1] = replaceName
updatePayloadValue[(i*3)+2] = replaceDescription
}
replaceDescription := map[string]interface{}{
"op": "replace",
"path": fmt.Sprintf("/variations/%d/description", i),
"value": transformedVariations[i].Description,
_, err = client.Patch(getFlagUrl(project, key), updatePayloadValue, []int{200}, NUMBER_OF_RETRY)
if err != nil {
return err
}
payloadValue[i*3] = replaceValue
payloadValue[(i*3)+1] = replaceName
payloadValue[(i*3)+2] = replaceDescription
}
_, err = client.Patch(getFlagUrl(project, key), payloadValue, []int{200}, NUMBER_OF_RETRY)
if err != nil {
return err
}

//Add new variations
if newNumberOfVariation > actualNumberOfVariation {
var payloadValue []interface{} = make([]interface{}, newNumberOfVariation - actualNumberOfVariation)
for i := actualNumberOfVariation; i < newNumberOfVariation; i++ {

payloadValue[i] = map[string]interface{}{
if newNumberOfVariation >= actualNumberOfVariation {
//Update values off existing variations
var updatePayloadValue []interface{} = make([]interface{}, 3*actualNumberOfVariation)
for i := 0; i < actualNumberOfVariation; i++ {
replaceValue := map[string]interface{}{
"op": "replace",
"path": fmt.Sprintf("/variations/%d/value", i),
"value": transformedVariations[i].Value,
}
replaceName := map[string]interface{}{
"op": "replace",
"path": fmt.Sprintf("/variations/%d/name", i),
"value": transformedVariations[i].Name,
}
replaceDescription := map[string]interface{}{
"op": "replace",
"path": fmt.Sprintf("/variations/%d/description", i),
"value": transformedVariations[i].Description,
}
updatePayloadValue[i*3] = replaceValue
updatePayloadValue[(i*3)+1] = replaceName
updatePayloadValue[(i*3)+2] = replaceDescription
}
_, err = client.Patch(getFlagUrl(project, key), updatePayloadValue, []int{200}, NUMBER_OF_RETRY)
if err != nil {
return err
}

var createPayloadValue []interface{} = make([]interface{}, newNumberOfVariation - actualNumberOfVariation)
for i := 0; i < len(createPayloadValue); i++ {
createPayloadValue[i] = map[string]interface{}{
"op": "add",
"path": fmt.Sprintf("/variations/%d", i),
"value": transformedVariations[i],
"path": fmt.Sprintf("/variations/%d", actualNumberOfVariation + i),
"value": transformedVariations[actualNumberOfVariation + i],
}
}
_, err = client.Patch(getFlagUrl(project, key), payloadValue, []int{200}, NUMBER_OF_RETRY)
_, err = client.Patch(getFlagUrl(project, key), createPayloadValue, []int{200}, NUMBER_OF_RETRY)
if err != nil {
return err
}
}

return nil
}

Expand Down

0 comments on commit 7dad605

Please sign in to comment.