From 905f1209d044a9289f4f2c578ce74d9b08545c6b Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Thu, 30 May 2024 07:36:27 +0100 Subject: [PATCH] create an enum type for status content type --- internal/client/statuses.go | 20 ++++++++--------- internal/executor/create.go | 9 ++++++-- internal/executor/errors.go | 10 ++++++++- internal/model/status.go | 43 +++++++++++++++++++++++++++++++++++-- 4 files changed, 67 insertions(+), 15 deletions(-) diff --git a/internal/client/statuses.go b/internal/client/statuses.go index aaea8bf..57dc85d 100644 --- a/internal/client/statuses.go +++ b/internal/client/statuses.go @@ -26,16 +26,16 @@ func (g *Client) GetStatus(statusID string) (model.Status, error) { } type CreateStatusForm struct { - Content string `json:"status"` - ContentType string `json:"content_type"` - Language string `json:"language"` - SpoilerText string `json:"spoiler_text"` - Boostable bool `json:"boostable"` - Federated bool `json:"federated"` - Likeable bool `json:"likeable"` - Replyable bool `json:"replyable"` - Sensitive bool `json:"sensitive"` - Visibility model.StatusVisibility `json:"visibility"` + Content string `json:"status"` + Language string `json:"language"` + SpoilerText string `json:"spoiler_text"` + Boostable bool `json:"boostable"` + Federated bool `json:"federated"` + Likeable bool `json:"likeable"` + Replyable bool `json:"replyable"` + Sensitive bool `json:"sensitive"` + ContentType model.StatusContentType `json:"content_type"` + Visibility model.StatusVisibility `json:"visibility"` } func (g *Client) CreateStatus(form CreateStatusForm) (model.Status, error) { diff --git a/internal/executor/create.go b/internal/executor/create.go index c067d35..4cd9f6e 100644 --- a/internal/executor/create.go +++ b/internal/executor/create.go @@ -40,7 +40,7 @@ func NewCreateExecutor(tlf TopLevelFlags, name, summary string) *CreateExecutor createExe.BoolVar(&createExe.replyable, flagReplyable, true, "specify if the status can be replied to") createExe.BoolVar(&createExe.sensitive, flagSensitive, false, "specify if the status should be marked as sensitive") createExe.StringVar(&createExe.content, flagContent, "", "the content of the status to create") - createExe.StringVar(&createExe.contentType, flagContentType, "text/plain", "the type that the contents should be parsed from (valid values are plain and markdown)") + createExe.StringVar(&createExe.contentType, flagContentType, "plain", "the type that the contents should be parsed from (valid values are plain and markdown)") createExe.StringVar(&createExe.language, flagLanguage, "", "the ISO 639 language code for this status") createExe.StringVar(&createExe.spoilerText, flagSpoilerText, "", "the text to display as the status' warning or subject") createExe.StringVar(&createExe.visibility, flagVisibility, "", "the visibility of the posted status") @@ -129,9 +129,14 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error { return InvalidStatusVisibilityError{Visibility: visibility} } + parsedContentType := model.ParseStatusContentType(c.contentType) + if parsedContentType == model.StatusContentTypeUnknown { + return InvalidStatusContentTypeError{ContentType: c.contentType} + } + form := client.CreateStatusForm{ Content: c.content, - ContentType: c.contentType, + ContentType: parsedContentType, Language: language, SpoilerText: c.spoilerText, Boostable: c.boostable, diff --git a/internal/executor/errors.go b/internal/executor/errors.go index b9133b2..e27a2f2 100644 --- a/internal/executor/errors.go +++ b/internal/executor/errors.go @@ -59,5 +59,13 @@ type InvalidStatusVisibilityError struct { } func (e InvalidStatusVisibilityError) Error() string { - return "'" + e.Visibility + "' is an invalid status visibility value" + return "'" + e.Visibility + "' is an invalid status visibility (valid values are public, unlisted, private, mutuals_only and direct)" +} + +type InvalidStatusContentTypeError struct { + ContentType string +} + +func (e InvalidStatusContentTypeError) Error() string { + return "'" + e.ContentType + "' is an invalid status content type (valid values are plain and markdown)" } diff --git a/internal/model/status.go b/internal/model/status.go index 44bfe2b..279c329 100644 --- a/internal/model/status.go +++ b/internal/model/status.go @@ -225,8 +225,8 @@ func (s StatusVisibility) String() string { return "unknown" } -func ParseStatusVisibility(visibility string) StatusVisibility { - switch visibility { +func ParseStatusVisibility(value string) StatusVisibility { + switch value { case "public": return StatusVisibilityPublic case "private": @@ -265,3 +265,42 @@ func (s *StatusVisibility) UnmarshalJSON(data []byte) error { return nil } + +type StatusContentType int + +const ( + StatusContentTypePlainText StatusContentType = iota + StatusContentTypeMarkdown + StatusContentTypeUnknown +) + +func (s StatusContentType) String() string { + switch s { + case StatusContentTypePlainText: + return "text/plain" + case StatusContentTypeMarkdown: + return "text/markdown" + } + + return "unknown" +} + +func ParseStatusContentType(value string) StatusContentType { + switch value { + case "plain", "text/plain": + return StatusContentTypePlainText + case "markdown", "text/markdown": + return StatusContentTypeMarkdown + } + + return StatusContentTypeUnknown +} + +func (s StatusContentType) MarshalJSON() ([]byte, error) { + value := s.String() + if value == "unknown" { + return nil, fmt.Errorf("%q is not a valid status content type", value) + } + + return json.Marshal(value) +}