diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
commit | b09c6d56832eb1718c07d74abf3bc6ae3fe4e030 (patch) | |
tree | d2caec2610d4ea887803ec9e9c3cd77136c448ba /pkg/types/comment_type.go | |
parent | Initial commit. (diff) | |
download | icingadb-upstream.tar.xz icingadb-upstream.zip |
Adding upstream version 1.1.0.upstream/1.1.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pkg/types/comment_type.go')
-rw-r--r-- | pkg/types/comment_type.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/pkg/types/comment_type.go b/pkg/types/comment_type.go new file mode 100644 index 0000000..8aed475 --- /dev/null +++ b/pkg/types/comment_type.go @@ -0,0 +1,79 @@ +package types + +import ( + "database/sql/driver" + "encoding" + "encoding/json" + "github.com/icinga/icingadb/internal" + "github.com/pkg/errors" + "strconv" +) + +// CommentType specifies a comment's origin's kind. +type CommentType uint8 + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (ct *CommentType) UnmarshalJSON(data []byte) error { + var i uint8 + if err := internal.UnmarshalJSON(data, &i); err != nil { + return err + } + + c := CommentType(i) + if _, ok := commentTypes[c]; !ok { + return badCommentType(data) + } + + *ct = c + return nil +} + +// UnmarshalText implements the encoding.TextUnmarshaler interface. +func (ct *CommentType) UnmarshalText(text []byte) error { + s := string(text) + + i, err := strconv.ParseUint(s, 10, 64) + if err != nil { + return internal.CantParseUint64(err, s) + } + + c := CommentType(i) + if uint64(c) != i { + // Truncated due to above cast, obviously too high + return badCommentType(s) + } + + if _, ok := commentTypes[c]; !ok { + return badCommentType(s) + } + + *ct = c + return nil +} + +// Value implements the driver.Valuer interface. +func (ct CommentType) Value() (driver.Value, error) { + if v, ok := commentTypes[ct]; ok { + return v, nil + } else { + return nil, badCommentType(ct) + } +} + +// badCommentType returns an error about a syntactically, but not semantically valid CommentType. +func badCommentType(t interface{}) error { + return errors.Errorf("bad comment type: %#v", t) +} + +// commentTypes maps all valid CommentType values to their SQL representation. +var commentTypes = map[CommentType]string{ + 1: "comment", + 4: "ack", +} + +// Assert interface compliance. +var ( + _ json.Unmarshaler = (*CommentType)(nil) + _ encoding.TextUnmarshaler = (*CommentType)(nil) + _ driver.Valuer = CommentType(0) +) |