webform

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 31ef635290cad3679b29e49b114d47660c054388
parent 9e471a6dabf9edd2d5734f57b9f9c7b06c2029dc
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun, 21 Jul 2024 16:31:01 +0900

format answers

Diffstat:
Mmain.go | 103+++++++++++++++++++++++++++++--------------------------------------------------
1 file changed, 38 insertions(+), 65 deletions(-)

diff --git a/main.go b/main.go @@ -13,9 +13,8 @@ import ( // An Entry is either a Part or a Question. type Entry interface { isPart() bool - fillAns(map[string][]string) error + makeAns(map[string][]string) ([]Ans, error) HTML() string - copy() Entry } type Part struct { @@ -31,24 +30,17 @@ func (p *Part) HTML() string { } return html + "</fieldset>\n" } -func (p *Part) fillAns(ans map[string][]string) error { + +func (p *Part) makeAns(ans map[string][]string) ([]Ans, error) { + var a []Ans for _, e := range p.Ent { - if err := e.fillAns(ans); err != nil { - return err - } - } - return nil -} -func (p *Part) copy() Entry { - pp := new(Part) - pp.Q = p.Q - if p.Ent != nil { - pp.Ent = make([]Entry, len(p.Ent)) - for i, e := range p.Ent { - pp.Ent[i] = e.copy() + aa, err := e.makeAns(ans) + if err != nil { + return nil, err } + a = append(a, aa...) } - return pp + return a, nil } type Question struct { @@ -94,95 +86,79 @@ func (q *Question) HTML() string { } } -func (q *Question) fillAns(ans map[string][]string) error { +func (q *Question) makeAns(ans map[string][]string) ([]Ans, error) { a, ok := ans[q.Qid] if !ok { - return fmt.Errorf("answer for %s not found.", q.Qid) + a = []string{""} } switch q.T { case "check": + var l []string for _, i := range a { n, err := strconv.Atoi(i) if err != nil { - return fmt.Errorf("invalid answer %q for %s.", + return nil, fmt.Errorf("invalid answer %q for %s.", i, q.Qid) } if n < 0 || len(q.CheckList) <= n { - return fmt.Errorf("invalid answer %q for %s.", + return nil, fmt.Errorf("invalid answer %q for %s.", n, q.Qid) } - q.CheckAns = append(q.CheckAns, q.CheckList[n]) + l = append(l, q.CheckList[n]) } - return nil + return []Ans{CheckAns{Qid: q.Qid, Type: q.T, Ans: l}}, nil case "radio": if len(a) != 1 { - return fmt.Errorf("length of radio answer for %s is %d.\n", + return nil, fmt.Errorf("length of radio answer for %s is %d.\n", q.Qid, len(a)) } n, err := strconv.Atoi(a[0]) if err != nil { - return fmt.Errorf("invalid answer %q for %s.", + return nil, fmt.Errorf("invalid answer %q for %s.", a[0], q.Qid) } if n < 0 || len(q.RadioList) <= n { - return fmt.Errorf("invalid answer %q for %s.", + return nil, fmt.Errorf("invalid answer %q for %s.", n, q.Qid) } - q.RadioAns = q.RadioList[n] - return nil + // TODO: other + return []Ans{RadioAns{Qid: q.Qid, Type: q.T, Ans: a[0]}}, nil case "text": if len(a) != 1 { - return fmt.Errorf("length of text answer for %s is %d.\n", + return nil, fmt.Errorf("length of text answer for %s is %d.\n", q.Qid, len(a)) } - q.TextAns = a[0] - return nil + return []Ans{TextAns{Qid: q.Qid, Type: q.T, Ans: a[0]}}, nil default: panic(fmt.Errorf("unknown question type: %v", q.T)) } } -func (q *Question) copy() Entry { - qq := new(Question) - qq.Q = q.Q - qq.Qid = q.Qid - qq.T = q.T - qq.TextAns = q.TextAns - if q.RadioList != nil { - qq.RadioList = make([]string, len(q.RadioList)) - copy(qq.RadioList, q.RadioList) - } - qq.RadioAns = q.RadioAns - if q.CheckList != nil { - qq.CheckList = make([]string, len(q.CheckList)) - copy(qq.CheckList, q.CheckList) - } - if q.CheckAns != nil { - qq.CheckAns = make([]string, len(q.CheckAns)) - copy(qq.CheckAns, q.CheckAns) - } - qq.OtherAns = q.OtherAns - return qq -} - type Ans interface { } type TextAns struct { Qid string + Type string Ans string } type RadioAns struct{ Qid string + Type string Ans string } type CheckAns struct{ Qid string + Type string Ans []string } +func formatAns(q Part, ans map[string][]string) ([]byte, error) { + return nil,nil +} + var Q Part = Part{ Q: "きのかわ弦楽合奏団第7回定期演奏会 アンケート", Ent: []Entry{ @@ -253,23 +229,20 @@ func handler(w http.ResponseWriter, r *http.Request) { return } fmt.Fprint(w, exitPage) - answer, err := json.MarshalIndent(r.PostForm, "", " ") + a, err := Q.makeAns(r.PostForm) if err != nil { - log.Printf("error: parse form to json: %v\n", err) + log.Printf("makeAns: %v\n", err) return } - log.Printf("answer json: %v\n", string(answer)) - QQ := Q.copy() - if err := QQ.fillAns(r.PostForm); err != nil { - log.Printf("fillAns: %v\n", err) - return - } - q, err := json.Marshal(QQ) + log.Printf("makeAns: %q\n", a) + A, err := json.MarshalIndent(a, "", " ") if err != nil { - log.Printf("error: marshall: %v\n", err) + log.Printf("marchalIndent: %v\n", err) return } - bufc <- q + log.Printf("A: %v\n", string(A)) + + bufc <- A } else { fmt.Fprint(w, header+Q.HTML()+footer) }