35 lines
755 B
Go
35 lines
755 B
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
// UnmarshalJSON accepts both GoChat's descriptive article statuses and the
|
|
// integer enum values sent by the Chatwoot frontend.
|
|
func (s *ArticleStatus) UnmarshalJSON(data []byte) error {
|
|
var status string
|
|
if err := json.Unmarshal(data, &status); err == nil {
|
|
*s = ArticleStatus(status)
|
|
return nil
|
|
}
|
|
|
|
var statusCode int
|
|
if err := json.Unmarshal(data, &statusCode); err != nil {
|
|
return fmt.Errorf("article status must be draft, published, archived, 0, 1, or 2")
|
|
}
|
|
|
|
switch statusCode {
|
|
case 0:
|
|
*s = ArticleStatusDraft
|
|
case 1:
|
|
*s = ArticleStatusPublished
|
|
case 2:
|
|
*s = ArticleStatusArchived
|
|
default:
|
|
return fmt.Errorf("invalid article status code %d", statusCode)
|
|
}
|
|
|
|
return nil
|
|
}
|