From 31271ad8b1c3bcdf17c95d2c538a2a591dabce8b Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 7 Apr 2023 08:38:08 +0300 Subject: [PATCH] [#47] netmap: Make PlacementPolicy.WriteStringTo() pass nofunlen, cogognit REP statement is obligatory, thus insert newline before each statement after REP. Signed-off-by: Evgenii Stratonikov --- netmap/policy.go | 47 ++++++++--------------------------------------- 1 file changed, 8 insertions(+), 39 deletions(-) diff --git a/netmap/policy.go b/netmap/policy.go index 114866f5..48c5ca26 100644 --- a/netmap/policy.go +++ b/netmap/policy.go @@ -393,49 +393,28 @@ func (p *PlacementPolicy) AddFilters(fs ...Filter) { // the result into w. Returns w's errors directly. // // See also DecodeString. -// -// nolint: funlen, gocognit func (p PlacementPolicy) WriteStringTo(w io.StringWriter) (err error) { - writtenSmth := false - - writeLnIfNeeded := func() error { - if writtenSmth { - _, err = w.WriteString("\n") - return err - } - - writtenSmth = true - - return nil - } + delim := "" for i := range p.replicas { - err = writeLnIfNeeded() - if err != nil { - return err - } - c := p.replicas[i].GetCount() s := p.replicas[i].GetSelector() if s != "" { - _, err = w.WriteString(fmt.Sprintf("REP %d IN %s", c, s)) + _, err = w.WriteString(fmt.Sprintf("%sREP %d IN %s", delim, c, s)) } else { - _, err = w.WriteString(fmt.Sprintf("REP %d", c)) + _, err = w.WriteString(fmt.Sprintf("%sREP %d", delim, c)) } if err != nil { return err } + + delim = "\n" } if p.backupFactor > 0 { - err = writeLnIfNeeded() - if err != nil { - return err - } - - _, err = w.WriteString(fmt.Sprintf("CBF %d", p.backupFactor)) + _, err = w.WriteString(fmt.Sprintf("\nCBF %d", p.backupFactor)) if err != nil { return err } @@ -444,12 +423,7 @@ func (p PlacementPolicy) WriteStringTo(w io.StringWriter) (err error) { var s string for i := range p.selectors { - err = writeLnIfNeeded() - if err != nil { - return err - } - - _, err = w.WriteString(fmt.Sprintf("SELECT %d", p.selectors[i].GetCount())) + _, err = w.WriteString(fmt.Sprintf("\nSELECT %d", p.selectors[i].GetCount())) if err != nil { return err } @@ -488,12 +462,7 @@ func (p PlacementPolicy) WriteStringTo(w io.StringWriter) (err error) { } for i := range p.filters { - err = writeLnIfNeeded() - if err != nil { - return err - } - - _, err = w.WriteString("FILTER ") + _, err = w.WriteString("\nFILTER ") if err != nil { return err }