Skip to content

Commit

Permalink
feat: Update Post and LegacyPost models to support omitempty for opti…
Browse files Browse the repository at this point in the history
…onal fields
  • Loading branch information
Laisky committed Nov 13, 2024
1 parent 0799900 commit 0b34dbe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 8 additions & 4 deletions internal/web/blog/model/posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ type Post struct {
// Hidden whether the post is hidden or not
Hidden bool `bson:"hidden" json:"hidden"`
// I18N internationalization of the post
I18N PostI18N `bson:"i18n" json:"i18n"`
I18N PostI18N `bson:"i18n" json:"i18n,omitempty"`
// Language language of the post content or markdown
Language string `bson:"-" json:"language"`
Language string `bson:"-" json:"language,omitempty"`
// ArweaveId arweave id of the post
ArweaveId []ArweaveHistoryItem `bson:"arweave_id" json:"arweave_id"`
}
Expand All @@ -60,6 +60,10 @@ type legacyTime struct {
}

func (t legacyTime) Time() (time.Time, error) {
if t.Date == "" {
return time.Time{}, nil
}

return time.Parse(time.RFC3339, t.Date)
}

Expand Down Expand Up @@ -115,9 +119,9 @@ type LegacyPost struct {
// Hidden whether the post is hidden or not
Hidden bool `bson:"hidden" json:"hidden"`
// I18N internationalization of the post
I18N legacyI18N `bson:"i18n" json:"i18n"`
I18N legacyI18N `bson:"i18n" json:"i18n,omitempty"`
// Language language of the post content or markdown
Language string `bson:"-" json:"language"`
Language string `bson:"-" json:"language,omitempty"`
// ArweaveId arweave id of the post
ArweaveId []ArweaveHistoryItem `bson:"arweave_id" json:"arweave_id"`
}
Expand Down
7 changes: 4 additions & 3 deletions internal/web/blog/service/posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,17 @@ func (s *Blog) LoadPostHistory(ctx context.Context, fileID string, language mode
}

post := new(model.Post)
if err = json.NewDecoder(reader).Decode(post); err != nil {
err = json.NewDecoder(reader).Decode(post)
if err != nil || post.Content == "" {
// try legacy struct
reader, err := makeReader(cnt)
if err != nil {
return nil, errors.Wrap(err, "get reader")
}

legacyPost := new(model.LegacyPost)
if err2 := json.NewDecoder(reader).Decode(legacyPost); err2 != nil {
return nil, errors.Wrap(errors.Join(err, err2), "cannot decode post by legacy struct")
if err = json.NewDecoder(reader).Decode(legacyPost); err != nil {
return nil, errors.Wrap(err, "cannot decode post by legacy struct")
}

if post, err = legacyPost.Post(); err != nil {
Expand Down

0 comments on commit 0b34dbe

Please sign in to comment.