2023-06-06 15:14:33 +03:00
|
|
|
package meta
|
|
|
|
|
2023-12-17 11:42:39 +03:00
|
|
|
import "bytes"
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
type ChangedFields struct {
|
|
|
|
fieldNames map[string]struct{}
|
2023-12-17 11:42:39 +03:00
|
|
|
fieldsKey *bytes.Buffer
|
2023-06-06 15:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewChangedFields(fieldCount int) ChangedFields {
|
2023-12-17 11:42:39 +03:00
|
|
|
keyBuffer := bytes.NewBuffer(make([]byte, 0, fieldCount*2))
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
cf := ChangedFields{
|
|
|
|
fieldNames: make(map[string]struct{}, fieldCount),
|
2023-12-17 11:42:39 +03:00
|
|
|
fieldsKey: keyBuffer,
|
2023-06-06 15:14:33 +03:00
|
|
|
}
|
|
|
|
return cf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *ChangedFields) Reset() {
|
|
|
|
if cf.fieldNames == nil {
|
|
|
|
cf.fieldNames = make(map[string]struct{})
|
|
|
|
} else {
|
|
|
|
for k := range cf.fieldNames {
|
|
|
|
delete(cf.fieldNames, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf ChangedFields) Changed(field string) bool {
|
|
|
|
_, ok := cf.fieldNames[field]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *ChangedFields) SetChanged(fields ...string) {
|
2023-06-17 10:45:52 +03:00
|
|
|
if cf.fieldNames == nil {
|
|
|
|
cf.fieldNames = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
for _, field := range fields {
|
2023-12-17 11:42:39 +03:00
|
|
|
if _, exists := cf.fieldNames[field]; exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
cf.fieldNames[field] = struct{}{}
|
2023-12-17 11:42:39 +03:00
|
|
|
cf.fieldsKey.WriteString(field)
|
2023-06-06 15:14:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf ChangedFields) Empty() bool {
|
|
|
|
return len(cf.fieldNames) == 0
|
|
|
|
}
|
2023-06-17 10:45:52 +03:00
|
|
|
|
|
|
|
func (cf ChangedFields) IsNil() bool {
|
|
|
|
return cf.fieldNames == nil
|
|
|
|
}
|
2023-12-17 11:42:39 +03:00
|
|
|
|
|
|
|
func (cf ChangedFields) GetFieldsKey() string {
|
|
|
|
return cf.fieldsKey.String()
|
|
|
|
}
|