feed.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package model
  5. import (
  6. "fmt"
  7. "reflect"
  8. "time"
  9. )
  10. // Feed represents a feed in the database.
  11. type Feed struct {
  12. ID int64 `json:"id"`
  13. UserID int64 `json:"user_id"`
  14. FeedURL string `json:"feed_url"`
  15. SiteURL string `json:"site_url"`
  16. Title string `json:"title"`
  17. CheckedAt time.Time `json:"checked_at,omitempty"`
  18. EtagHeader string `json:"etag_header,omitempty"`
  19. LastModifiedHeader string `json:"last_modified_header,omitempty"`
  20. ParsingErrorMsg string `json:"parsing_error_message,omitempty"`
  21. ParsingErrorCount int `json:"parsing_error_count,omitempty"`
  22. Category *Category `json:"category,omitempty"`
  23. Entries Entries `json:"entries,omitempty"`
  24. Icon *FeedIcon `json:"icon,omitempty"`
  25. }
  26. func (f *Feed) String() string {
  27. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  28. f.ID,
  29. f.UserID,
  30. f.FeedURL,
  31. f.SiteURL,
  32. f.Title,
  33. f.Category,
  34. )
  35. }
  36. // Merge combine src to the current struct
  37. func (f *Feed) Merge(src *Feed) {
  38. src.ID = f.ID
  39. src.UserID = f.UserID
  40. new := reflect.ValueOf(src).Elem()
  41. for i := 0; i < new.NumField(); i++ {
  42. field := new.Field(i)
  43. switch field.Interface().(type) {
  44. case int64:
  45. value := field.Int()
  46. if value != 0 {
  47. reflect.ValueOf(f).Elem().Field(i).SetInt(value)
  48. }
  49. case string:
  50. value := field.String()
  51. if value != "" {
  52. reflect.ValueOf(f).Elem().Field(i).SetString(value)
  53. }
  54. }
  55. }
  56. }
  57. // Feeds is a list of feed
  58. type Feeds []*Feed