Skip to content

Commit

Permalink
chore: use strings.Builder instead of concatenation
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <norman@samourai.coop>
  • Loading branch information
n0izn0iz committed Apr 13, 2024
1 parent a6689df commit 165c1a6
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 62 deletions.
27 changes: 18 additions & 9 deletions examples/gno.land/p/demo/teritori/dao_core/dao_core.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"std"
"strconv"
"strings"

dao_interfaces "gno.land/p/demo/teritori/dao_interfaces"
)
Expand Down Expand Up @@ -122,22 +123,30 @@ func (d *daoCore) ActiveProposalModuleCount() int {
}

func (d *daoCore) Render(path string) string {
s := "# DAO Core\n"
sb := strings.Builder{}
sb.WriteString("# DAO Core\n")
votingInfo := d.votingModule.Info()
s += "## Voting Module: " + votingInfo.String() + "\n"
s += d.votingModule.Render("")
s += "## Supported Messages:\n"
s += d.registry.Render()
s += "## Proposal Modules:\n"
sb.WriteString("## Voting Module: ")
sb.WriteString(votingInfo.String())
sb.WriteRune('\n')
sb.WriteString(d.votingModule.Render(""))
sb.WriteString("## Supported Messages:\n")
sb.WriteString(d.registry.Render())
sb.WriteString("## Proposal Modules:\n")
for i, propMod := range d.proposalModules {
if !propMod.Enabled {
continue
}
info := propMod.Module.Info()
s += "### #" + strconv.Itoa(i) + ": " + info.String() + "\n"
s += propMod.Module.Render("")
sb.WriteString("### #")
sb.WriteString(strconv.Itoa(i))
sb.WriteString(": ")
sb.WriteString(info.String())
sb.WriteRune('\n')
sb.WriteString(propMod.Module.Render(""))
}
return s + "\n"
sb.WriteRune('\n')
return sb.String()
}

func (d *daoCore) Registry() *dao_interfaces.MessagesRegistry {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dao_interfaces

import (
"strings"

"gno.land/p/demo/avl"
"gno.land/p/demo/json"
)
Expand Down Expand Up @@ -55,12 +57,14 @@ func (r *MessagesRegistry) ExecuteMessages(msgs []ExecutableMessage) {
}

func (r *MessagesRegistry) Render() string {
s := ""
sb := strings.Builder{}
r.handlers.Iterate("", "", func(key string, value interface{}) bool {
s += "- " + value.(MessageHandler).Type() + "\n"
sb.WriteString("- ")
sb.WriteString(value.(MessageHandler).Type())
sb.WriteRune('\n')
return false
})
return s
return sb.String()
}

type RegisterHandlerExecutableMessage struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dao_proposal_single
import (
"std"
"strconv"
"strings"

"gno.land/p/demo/avl"
"gno.land/p/demo/json"
Expand Down Expand Up @@ -139,53 +140,91 @@ func (d *DAOProposalSingle) Render(path string) string {
closeOnExecFailureStr = "Proposals will be closed if their execution fails"
}

thresholdStr := ""
sb := strings.Builder{}
sb.WriteString("Max voting period: ")
sb.WriteString(d.opts.MaxVotingPeriod.String())
sb.WriteString("\n\n")
sb.WriteString(minVotingPeriodStr)
sb.WriteString("\n\n")
sb.WriteString(executeStr)
sb.WriteString("\n\n")
sb.WriteString(revotingStr)
sb.WriteString("\n\n")
sb.WriteString(closeOnExecFailureStr)
sb.WriteString("\n\n")
switch d.opts.Threshold.(type) {
case *ThresholdThresholdQuorum:
threshold := d.opts.Threshold.(*ThresholdThresholdQuorum)
thresholdStr = "Threshold: " + threshold.Threshold.String() + "\n\n" +
"Quorum: " + threshold.Quorum.String()
sb.WriteString("Threshold: ")
sb.WriteString(threshold.Threshold.String())
sb.WriteString("\n\nQuorum: ")
sb.WriteString(threshold.Quorum.String())
case *ThresholdAbsolutePercentage:
threshold := d.opts.Threshold.(*ThresholdAbsolutePercentage)
sb.WriteString("Threshold (Absolute Percentage): ")
sb.WriteString(threshold.Value.String())
case *ThresholdAbsoluteCount:
threshold := d.opts.Threshold.(*ThresholdAbsoluteCount)
sb.WriteString("Threshold (Absolute Count): ")
sb.WriteString(strconv.FormatUint(uint64(*threshold), 10))
default:
panic("unsupported Threshold type")
}

proposalsStr := "Proposals:\n\n"
sb.WriteString("\n\n")
sb.WriteString("Proposals:\n\n")
for _, p := range d.proposals {
messagesStr := ""
sb.WriteString("\\#")
sb.WriteString(strconv.Itoa(p.ID))
sb.WriteString(": ")
sb.WriteString(p.Title)
sb.WriteString(":\n\n")
sb.WriteString(" Status: ")
sb.WriteString(p.Status.String())
sb.WriteString("\n\n")
sb.WriteString(" Proposed by ")
sb.WriteString(p.Proposer.String())
sb.WriteString("\n\n")
sb.WriteString(" ")
sb.WriteString(p.Description)
sb.WriteString("\n\n")
sb.WriteString(" Votes summary:")
sb.WriteString("\n\n")
sb.WriteString(" - Yes: ")
sb.WriteString(strconv.FormatUint(p.Votes.Yes, 10))
sb.WriteRune('\n')
sb.WriteString(" - No: ")
sb.WriteString(strconv.FormatUint(p.Votes.No, 10))
sb.WriteRune('\n')
sb.WriteString(" - Abstain: ")
sb.WriteString(strconv.FormatUint(p.Votes.Abstain, 10))
sb.WriteString("\n\n")
sb.WriteString(" Total: ")
sb.WriteString(strconv.FormatUint(p.Votes.Total(), 10))
sb.WriteString("\n\n")
sb.WriteString(" Messages:")
sb.WriteString("\n\n")
for _, m := range p.Messages {
messagesStr += m.(dao_interfaces.ExecutableMessage).Type() + "\n\n"
messagesStr += m.(dao_interfaces.ExecutableMessage).String() + "\n\n"
sb.WriteString(m.(dao_interfaces.ExecutableMessage).Type())
sb.WriteString("\n\n")
sb.WriteString(m.(dao_interfaces.ExecutableMessage).String())
sb.WriteString("\n\n")
}

proposalsStr += "\\#" + strconv.Itoa(p.ID) + ": " + p.Title + ":\n\n" +
" Status: " + p.Status.String() + "\n\n" +
" Proposed by " + p.Proposer.String() + "\n\n" +
" " + p.Description + "\n\n" +
" Votes summary:" + "\n\n" +
" - Yes: " + strconv.FormatUint(p.Votes.Yes, 10) + "\n" +
" - No: " + strconv.FormatUint(p.Votes.No, 10) + "\n" +
" - Abstain: " + strconv.FormatUint(p.Votes.Abstain, 10) + "\n\n" +
" Total: " + strconv.FormatUint(p.Votes.Total(), 10) + "\n\n" +
" Messages:\n\n" +
messagesStr +
" Votes:\n\n"
sb.WriteString(" Votes:")
sb.WriteString("\n\n")

p.Ballots.Iterate("", "", func(k string, v interface{}) bool {
ballot := v.(Ballot)
proposalsStr += k + " voted " + ballot.String() + "\n\n"
sb.WriteString(k)
sb.WriteString(" voted ")
sb.WriteString(ballot.String())
sb.WriteString("\n\n")
return false
})

proposalsStr += "\n"
sb.WriteRune('\n')
}

return "Max voting period: " + d.opts.MaxVotingPeriod.String() + "\n\n" +
minVotingPeriodStr + "\n\n" +
executeStr + "\n\n" +
revotingStr + "\n\n" +
closeOnExecFailureStr + "\n\n" +
thresholdStr + "\n\n" +
proposalsStr
return sb.String()
}

func (d *DAOProposalSingle) Core() dao_interfaces.IDAOCore {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dao_proposal_single

import (
"strconv"
"strings"

"gno.land/p/demo/json"
"gno.land/p/demo/teritori/jsonutil"
Expand Down Expand Up @@ -45,12 +46,15 @@ func (p *PercentageThresholdMajority) ToJSON() *json.Node {
type PercentageThresholdPercent uint16 // 4 decimals fixed point

func (p *PercentageThresholdPercent) String() string {
s := strconv.FormatUint(uint64(*p)/100, 10)
sb := strings.Builder{}
sb.WriteString(strconv.FormatUint(uint64(*p)/100, 10))
decPart := uint64(*p) % 100
if decPart != 0 {
s += "." + strconv.FormatUint(decPart, 10)
sb.WriteRune('.')
sb.WriteString(strconv.FormatUint(decPart, 10))
}
return s + "%"
sb.WriteRune('%')
return sb.String()
}

func (p *PercentageThresholdPercent) FromJSON(ast *json.Node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dao_voting_group
import (
"std"
"strconv"
"strings"

"gno.land/p/demo/json"
dao_interfaces "gno.land/p/demo/teritori/dao_interfaces"
Expand Down Expand Up @@ -140,12 +141,22 @@ func (g *VotingGroup) GetMembers(start, end string, limit uint64, height int64)
}

func (v *VotingGroup) Render(path string) string {
s := "Member count: " + strconv.FormatUint(uint64(v.MemberCount(havl.Latest)), 10) + "\n\n"
s += "Total power: " + strconv.FormatUint(v.TotalPowerAtHeight(havl.Latest), 10) + "\n\n"
s += "Members:\n"
sb := strings.Builder{}
sb.WriteString("Member count: ")
sb.WriteString(strconv.FormatUint(uint64(v.MemberCount(havl.Latest)), 10))
sb.WriteString("\n\n")
sb.WriteString("Total power: ")
sb.WriteString(strconv.FormatUint(v.TotalPowerAtHeight(havl.Latest), 10))
sb.WriteString("\n\n")
sb.WriteString("Members:\n")
v.powerByAddr.Iterate("", "", havl.Latest, func(k string, v interface{}) bool {
s += "- " + k + ": " + strconv.FormatUint(v.(uint64), 10) + "\n"
sb.WriteString("- ")
sb.WriteString(k)
sb.WriteString(": ")
sb.WriteString(strconv.FormatUint(v.(uint64), 10))
sb.WriteRune('\n')
return false
})
return s + "\n"
sb.WriteRune('\n')
return sb.String()
}
30 changes: 18 additions & 12 deletions examples/gno.land/r/demo/teritori/tori/messages.gno
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ func (msg ExecutableMessageMintTori) Type() string {
}

func (msg *ExecutableMessageMintTori) String() string {
var ss []string
ss = append(ss, msg.Type())
s := "Recipient: " + string(msg.Recipient) + "\n"
s += "Amount: " + strconv.FormatUint(msg.Amount, 10)
ss = append(ss, s)
return strings.Join(ss, "\n---\n")
sb := strings.Builder{}
sb.WriteString(msg.Type())
sb.WriteString("\n---\n")
sb.WriteString("Recipient: ")
sb.WriteString(string(msg.Recipient))
sb.WriteRune('\n')
sb.WriteString("Amount: ")
sb.WriteString(strconv.FormatUint(msg.Amount, 10))
return sb.String()
}

func (msg *ExecutableMessageMintTori) FromJSON(ast *json.Node) {
Expand Down Expand Up @@ -82,12 +85,15 @@ func (msg ExecutableMessageBurnTori) Type() string {
}

func (msg *ExecutableMessageBurnTori) String() string {
var ss []string
ss = append(ss, msg.Type())
s := "Target: " + string(msg.Target) + "\n"
s += "Amount: " + strconv.FormatUint(msg.Amount, 10)
ss = append(ss, s)
return strings.Join(ss, "\n---\n")
sb := strings.Builder{}
sb.WriteString(msg.Type())
sb.WriteString("\n---\n")
sb.WriteString("Target: ")
sb.WriteString(string(msg.Target))
sb.WriteRune('\n')
sb.WriteString("Amount: ")
sb.WriteString(strconv.FormatUint(msg.Amount, 10))
return sb.String()
}

func (msg *ExecutableMessageBurnTori) FromJSON(ast *json.Node) {
Expand Down

0 comments on commit 165c1a6

Please sign in to comment.