meta/changed_fields.go

61 lines
1.1 KiB
Go
Raw Normal View History

package meta
2023-12-17 11:42:39 +03:00
import "bytes"
type ChangedFields struct {
fieldNames map[string]struct{}
2023-12-17 11:42:39 +03:00
fieldsKey *bytes.Buffer
}
func NewChangedFields(fieldCount int) ChangedFields {
2023-12-17 11:42:39 +03:00
keyBuffer := bytes.NewBuffer(make([]byte, 0, fieldCount*2))
cf := ChangedFields{
fieldNames: make(map[string]struct{}, fieldCount),
2023-12-17 11:42:39 +03:00
fieldsKey: keyBuffer,
}
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) {
if cf.fieldNames == nil {
cf.fieldNames = make(map[string]struct{})
}
for _, field := range fields {
2023-12-17 11:42:39 +03:00
if _, exists := cf.fieldNames[field]; exists {
continue
}
cf.fieldNames[field] = struct{}{}
2023-12-17 11:42:39 +03:00
cf.fieldsKey.WriteString(field)
}
}
func (cf ChangedFields) Empty() bool {
return len(cf.fieldNames) == 0
}
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()
}