/
models.go
96 lines (85 loc) · 2.85 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package db
import (
"encoding/json"
"github.com/frouioui/tagenal/api/articles/pb"
)
// ArticleArray represents an array of Article.
// Implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler.
type ArticleArray []Article
// Article struct refers to the article table of the
// Vitess MySQl cluster, in the articles keyspace.
type Article struct {
ID int64 `json:"id"`
Timestamp int64 `json:"timestamp"`
AID string `json:"aid"`
Title string `json:"title"`
Category string `json:"category"`
Abstract string `json:"abstract"`
ArticleTags string `json:"article_tags"`
Authors string `json:"authors"`
Language string `json:"language"`
Text string `json:"text"`
Image string `json:"image"`
Video string `json:"video"`
}
// ProtoArticle transforms an Article into a the auto-generated
// pb.Article structure from protobuf.
func (u *Article) ProtoArticle() *pb.Article {
return &pb.Article{
ID: u.ID,
Timestamp: u.Timestamp,
AID: u.AID,
Title: u.Title,
Category: u.Category,
Abstract: u.Abstract,
ArticleTags: u.ArticleTags,
Authors: u.Authors,
Language: u.Language,
Text: u.Text,
Image: u.Image,
Video: u.Video,
}
}
// ProtoArticleToArticle transforms an auto-generated pb.Article from
// protobuf into the package implementation of Article.
func ProtoArticleToArticle(pbarticle *pb.Article) (article Article) {
return Article{
ID: pbarticle.ID,
Timestamp: pbarticle.Timestamp,
AID: pbarticle.AID,
Title: pbarticle.Title,
Category: pbarticle.Category,
Abstract: pbarticle.Abstract,
ArticleTags: pbarticle.ArticleTags,
Authors: pbarticle.Authors,
Language: pbarticle.Language,
Text: pbarticle.Text,
Image: pbarticle.Image,
Video: pbarticle.Video,
}
}
// ArticlesToProtoArticles transforms an array of Articles into
// an array of pb.Articles which are auto-generated from protobuf.
func ArticlesToProtoArticles(articles []Article) *pb.Articles {
pbarticles := &pb.Articles{Articles: make([]*pb.Article, len(articles))}
for i, a := range articles {
pbarticles.Articles[i] = a.ProtoArticle()
}
return pbarticles
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (u *Article) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, &u)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (u *Article) MarshalBinary() (data []byte, err error) {
return json.Marshal(u)
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (ua *ArticleArray) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, &ua)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (ua *ArticleArray) MarshalBinary() (data []byte, err error) {
return json.Marshal(ua)
}