Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: correctly escape placement options #37606

Merged
merged 6 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1541,51 +1541,57 @@ func writeSettingItemToBuilder(sb *strings.Builder, item string) {
}
sb.WriteString(item)
}
func writeSettingStringToBuilder(sb *strings.Builder, item string, value string) {
writeSettingItemToBuilder(sb, fmt.Sprintf("%s=\"%s\"", item, strings.ReplaceAll(value, "\"", "\\\"")))
}
func writeSettingIntegerToBuilder(sb *strings.Builder, item string, value uint64) {
writeSettingItemToBuilder(sb, fmt.Sprintf("%s=%d", item, value))
}

func (p *PlacementSettings) String() string {
sb := new(strings.Builder)
if len(p.PrimaryRegion) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("PRIMARY_REGION=\"%s\"", p.PrimaryRegion))
writeSettingStringToBuilder(sb, "PRIMARY_REGION", p.PrimaryRegion)
}

if len(p.Regions) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("REGIONS=\"%s\"", p.Regions))
writeSettingStringToBuilder(sb, "REGIONS", p.Regions)
}

if len(p.Schedule) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("SCHEDULE=\"%s\"", p.Schedule))
writeSettingStringToBuilder(sb, "SCHEDULE", p.Schedule)
}

if len(p.Constraints) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("CONSTRAINTS=\"%s\"", p.Constraints))
writeSettingStringToBuilder(sb, "CONSTRAINTS", p.Constraints)
}

if len(p.LeaderConstraints) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("LEADER_CONSTRAINTS=\"%s\"", p.LeaderConstraints))
writeSettingStringToBuilder(sb, "LEADER_CONSTRAINTS", p.LeaderConstraints)
}

if p.Voters > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("VOTERS=%d", p.Voters))
writeSettingIntegerToBuilder(sb, "VOTERS", p.Voters)
}

if len(p.VoterConstraints) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("VOTER_CONSTRAINTS=\"%s\"", p.VoterConstraints))
writeSettingStringToBuilder(sb, "VOTER_CONSTRAINTS", p.VoterConstraints)
}

if p.Followers > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("FOLLOWERS=%d", p.Followers))
writeSettingIntegerToBuilder(sb, "FOLLOWERS", p.Followers)
}

if len(p.FollowerConstraints) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("FOLLOWER_CONSTRAINTS=\"%s\"", p.FollowerConstraints))
writeSettingStringToBuilder(sb, "FOLLOWER_CONSTRAINTS", p.FollowerConstraints)
}

if p.Learners > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("LEARNERS=%d", p.Learners))
writeSettingIntegerToBuilder(sb, "LEARNERS", p.Learners)
}

if len(p.LearnerConstraints) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("LEARNER_CONSTRAINTS=\"%s\"", p.LearnerConstraints))
writeSettingStringToBuilder(sb, "LEARNER_CONSTRAINTS", p.LearnerConstraints)
}

return sb.String()
Expand Down
4 changes: 2 additions & 2 deletions parser/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,9 +680,9 @@ func TestPlacementSettingsString(t *testing.T) {
Voters: 3,
Followers: 2,
Learners: 1,
Constraints: "{+us-east-1:1,+us-east-2:1}",
Constraints: "{\"+us-east-1\":1,+us-east-2:1}",
}
require.Equal(t, "CONSTRAINTS=\"{+us-east-1:1,+us-east-2:1}\" VOTERS=3 FOLLOWERS=2 LEARNERS=1", settings.String())
require.Equal(t, "CONSTRAINTS=\"{\\\"+us-east-1\\\":1,+us-east-2:1}\" VOTERS=3 FOLLOWERS=2 LEARNERS=1", settings.String())
}

func TestPlacementSettingsClone(t *testing.T) {
Expand Down