/
InputField.go
117 lines (97 loc) · 2.8 KB
/
InputField.go
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
package model
import (
"database/sql"
"encoding/json"
"strconv"
"strings"
)
// InputField represents the InputField table inside the JW Library database
type InputField struct {
LocationID int
TextTag string
Value string
pseudoID int `ignore:"true"`
}
// ID returns the ID of the entry. As the InputField table does not have
// an ID, we are using a pseudoID, so the rest of the merge logic is
// still able to run as usual.
func (m *InputField) ID() int {
return m.pseudoID
}
// SetID sets the ID of the entry. As the InputField table does not have
// an ID, this function does nothing.
func (m *InputField) SetID(id int) {
}
// UniqueKey returns the key that makes this InputField unique,
// so it can be used as a key in a map.
func (m *InputField) UniqueKey() string {
var sb strings.Builder
sb.Grow(15)
sb.WriteString(strconv.FormatInt(int64(m.LocationID), 10))
sb.WriteString("_")
sb.WriteString(m.TextTag)
return sb.String()
}
// Equals checks if the InputField is equal to the given one.
func (m *InputField) Equals(m2 Model) bool {
if m2, ok := m2.(*InputField); ok {
return m.LocationID == m2.LocationID &&
m.TextTag == m2.TextTag &&
m.Value == m2.Value
}
return false
}
// RelatedEntries returns entries that are related to this one.
func (m *InputField) RelatedEntries(db *Database) Related {
result := Related{}
if location := db.FetchFromTable("Location", int(m.LocationID)); location != nil {
result.Location = location.(*Location)
}
return result
}
// PrettyPrint prints InputField in a human readable format and
// adds information about related entries if helpful.
func (m *InputField) PrettyPrint(db *Database) string {
var result string
if location := db.FetchFromTable("Location", m.LocationID); location != nil {
result += location.PrettyPrint(db) + "\n"
}
result += prettyPrint(m, []string{"TextTag", "Value"})
return result
}
// MarshalJSON returns the JSON encoding of the entry.
func (m InputField) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string `json:"type"`
LocationID int `json:"locationId"`
TextTag string `json:"textTag"`
Value string `json:"value"`
}{
Type: "InputField",
LocationID: m.LocationID,
TextTag: m.TextTag,
Value: m.Value,
})
}
func (m *InputField) tableName() string {
return "InputField"
}
func (m *InputField) idName() string {
return ""
}
func (m *InputField) scanRow(rows *sql.Rows) (Model, error) {
err := rows.Scan(&m.LocationID, &m.TextTag, &m.Value)
return m, err
}
// MakeSlice converts a slice of the generice interface model.
func (InputField) MakeSlice(mdl []Model) []*InputField {
result := make([]*InputField, len(mdl))
for i := range mdl {
if mdl[i] == nil {
continue
}
inputField := mdl[i].(*InputField)
result[i] = inputField
}
return result
}