summaryrefslogtreecommitdiff
path: root/generateHTTP.go
blob: 15cbb540c8f3a234f0eb10706103cbec44f81961 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"fmt"
	"io"
	"os"

	"go.wit.com/log"
)

func (pb *Files) makeHTTPFile(pf *File) error {
	newf, _ := os.OpenFile(pf.Filebase+".http.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	defer newf.Close()

	headerHTTP(newf, pf)

	fmt.Fprintf(newf, "// START HTTP\n")
	fmt.Fprintf(newf, "\n")

	/*
		FRUITS := pf.Bases.Name
		FRUIT := pf.Base.Name
		fruitVars := pf.Base.Vars
		pf.generateAutoTablePB(newf, FRUITS, FRUIT, fruitVars)
	*/

	for _, msg := range pf.allMsg() {
		if msg.DoHTTP {
			FRUITS := msg.Name
			fRUITS := untitle(FRUITS)

			httpSendReply(newf, FRUITS)
			httpPost(newf, FRUITS, fRUITS)
			log.Printf("func (p *%s) HttpPost(baseURL string, route string) (*%s, *httppb.HttpRequest, error)\n", FRUITS, FRUITS)
		}
	}

	fmt.Fprintf(newf, "// END HTTP\n")
	return nil
}

func headerHTTP(w io.Writer, pf *File) {
	// header must come first
	headerComment(w)

	fmt.Fprintf(w, "package %s\n", pf.Package)
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "import (")
	fmt.Fprintln(w, "	\"time\"")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	\"go.wit.com/lib/protobuf/httppb\"")
	fmt.Fprintln(w, ")")
	fmt.Fprintln(w, "")
}

func httpTest(w io.Writer, FRUITS string, fRUITS string, FRUIT string, fRUIT string) {
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) "+fRUITS+"Custom(w *guipb.Widget) {")
	fmt.Fprintln(w, "	row := mt.x."+FRUITS+"[w.Location.Y-1]")
	fmt.Fprintln(w, "	// log.Info(\"got to "+fRUITS+"Custom() with\", w.Location.X, w.Location.Y-1)")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	for _, sf := range mt.buttonFuncs {")
	fmt.Fprintln(w, "		if sf.order == int(w.Location.X) {")
	fmt.Fprintln(w, "			// log.Info(\"found order\", sf.order)")
	fmt.Fprintln(w, "			if sf.Custom != nil {")
	fmt.Fprintln(w, "				log.Info(\"doing Custom() func for button\")")
	fmt.Fprintln(w, "				sf.Custom(row)")
	fmt.Fprintln(w, "				return")
	fmt.Fprintln(w, "			}")
	fmt.Fprintln(w, "		}")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "	mt.CustomFunc(row)")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) Custom(f func(*"+FRUIT+")) {")
	fmt.Fprintln(w, "	mt.pb.RegisterCustom(mt."+fRUITS+"Custom)")
	fmt.Fprintln(w, "	mt.CustomFunc = f")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) GetUuid() string {")
	fmt.Fprintln(w, "	return mt.pb.Uuid")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "// END TABLE UPDATE")
}

func httpSendReply(w io.Writer, FRUITS string) {
	fmt.Fprintln(w, "func (p *"+FRUITS+") SendReply(w http.ResponseWriter, reqPB *httppb.HttpRequest) error {")
	fmt.Fprintln(w, "	data, err := p.Marshal()")
	fmt.Fprintln(w, "	if err != nil {")
	fmt.Fprintln(w, "		// reqPB.Errors = append(reqPB.Errors, log.Sprintf(, err))")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "	if len(data) == 0 {")
	fmt.Fprintln(w, "		// reqPB.Errors = append(reqPB.Errors, \"Patches PB data was nil/emtpy without Marsha() error\")")
	fmt.Fprintln(w, "		return nil")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "	_, err = w.Write(data)")
	fmt.Fprintln(w, "	if err != nil {")
	fmt.Fprintln(w, "		// reqPB.Errors = append(reqPB.Errors, log.Sprintf(, i, err))")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "	return err")
	fmt.Fprintln(w, "}")
}

func httpPost(w io.Writer, FRUITS string, fRUITS string) {
	fmt.Fprintln(w, "// Marshal protobuf, then http POST, then Unmarshal() to protobuf again")
	fmt.Fprintln(w, "func (p *"+FRUITS+") HttpPost(baseURL string, route string) (*"+FRUITS+", *httppb.HttpRequest, error) {")
	fmt.Fprintln(w, "	data, err := p.Marshal()")
	fmt.Fprintln(w, "	if err != nil {")
	fmt.Fprintln(w, "		return nil, nil, err")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	tmp := filepath.Join(\""+fRUITS+"\",route)")
	fmt.Fprintln(w, "	reqPB, err := httppb.DoPost(baseURL, tmp, data)")
	fmt.Fprintln(w, "	if reqPB == nil {")
	fmt.Fprintln(w, "		return nil, nil, err")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	if len(reqPB.ServerData) == 0 {")
	fmt.Fprintln(w, "		return nil, reqPB, fmt.Errorf(\"server returned len(data)=0\")")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	newpb := new("+FRUITS+")")
	fmt.Fprintln(w, "	err = newpb.Unmarshal(reqPB.ServerData)")
	fmt.Fprintln(w, "	return newpb, reqPB, err")
	fmt.Fprintln(w, "}")
}