summaryrefslogtreecommitdiff
path: root/generateHeader.go
blob: 78fb35966a0f13fdad6cac3b14a6ca93101503b8 (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
// 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/lib/gui/prep"
	"go.wit.com/log"
)

func pbHeaderComment(w io.Writer) {
	// technically this should be the first line and in this exact format:
	fmt.Fprintln(w, "// Code modified by go.wit.com/apps/autogenpb DO NOT EDIT.")
	fmt.Fprintln(w, "//     go install go.wit.com/apps/autogenpb@latest")
	fmt.Fprintln(w, "//")
	fmt.Fprintln(w, "// user defined Mutex locks were auto added")
	fmt.Fprintln(w, "//")
	fmt.Fprintln(w, "// This file was autogenerated with autogenpb:")
	fmt.Fprintln(w, "// ", prep.StandardVersion(ARGNAME, VERSION, BUILDTIME))
	fmt.Fprintln(w, "// Theese sort.pb.go and marshal.pb.go files are autogenerated")
	fmt.Fprintln(w, "// The autogenpb sources have example .proto files with instructions")
	fmt.Fprintln(w, "")
}

func headerComment(w io.Writer) {
	// technically this should be the first line and in this exact format:
	fmt.Fprintln(w, "// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT.")
	fmt.Fprintln(w, "//     go install go.wit.com/apps/autogenpb@latest")
	fmt.Fprintln(w, "//")
	fmt.Fprintln(w, "// This file was autogenerated with autogenpb:")
	fmt.Fprintln(w, "// ", prep.StandardVersion(ARGNAME, VERSION, BUILDTIME))
	fmt.Fprintln(w, "// Theese sort.pb.go and marshal.pb.go files are autogenerated")
	fmt.Fprintln(w, "// The autogenpb sources have example .proto files with instructions")
	fmt.Fprintln(w, "//")
	fmt.Fprintln(w, "")
}

func header(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, "	\"sort\"")
	fmt.Fprintln(w, "	\"sync\"")
	fmt.Fprintln(w, "	\"iter\"")
	fmt.Fprintln(w, "	\"go.wit.com/lib/config\"")
	fmt.Fprintln(w, ")")
	fmt.Fprintln(w, "")
}

func (pb *File) addNewFunc(w io.Writer) {
	var STRUCT string = pb.Bases.Name
	var UUID string = pb.Uuid
	var VERSION string = pb.Version
	if UUID == "" {
		log.Info("error: UUID == ''")
		os.Exit(-1)
	}
	if VERSION == "" {
		log.Info("error: Version == ''")
		os.Exit(-1)
	}
	fmt.Fprintln(w, "func (x *"+STRUCT+") fixUuid() {")
	fmt.Fprintln(w, "	if x == nil {")
	fmt.Fprintln(w, "		return")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "	if x.Uuid == \""+UUID+"\" {")
	fmt.Fprintln(w, "		return")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "	x.Uuid = \""+UUID+"\"")
	fmt.Fprintln(w, "	x.Version = \""+VERSION+" "+pb.GoPath+"\"")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "func New"+STRUCT+"() *"+STRUCT+"{")
	fmt.Fprintln(w, "	x := new("+STRUCT+")")
	fmt.Fprintln(w, "	x.Uuid = \""+UUID+"\"")
	fmt.Fprintln(w, "	x.Version = \""+VERSION+" "+pb.GoPath+"\"")
	fmt.Fprintln(w, "	return x")
	fmt.Fprintln(w, "}")
	if pb.DoSave {
		fmt.Fprintln(w, "")
		fmt.Fprintln(w, "func (pb *"+STRUCT+") Save() error {")
		fmt.Fprintln(w, "	return config.SavePB(pb)")
		fmt.Fprintln(w, "}")
		fmt.Fprintln(w, "")
		fmt.Fprintln(w, "func (pb *"+STRUCT+") Load() error {")
		fmt.Fprintln(w, "	return config.LoadPB(pb)")
		fmt.Fprintln(w, "}")
	}
}