1
0
dwelling-home/pkg/article/article.go

42 lines
824 B
Go

package article
import (
"bytes"
"errors"
"strconv"
"time"
)
type Article struct {
ID int64
Date time.Time
Title string
Description string
Body []byte
}
func New(body, meta []byte) (article *Article, err error) {
article = &Article{Body: body}
err = article.ParseMetadata(meta)
return
}
func (artcl *Article) ParseMetadata(data []byte) error {
lines := bytes.Split(data, []byte{'\n'})
if len(lines) != 4 {
return errors.New("metadata file is not complete. 4 lines (id, title, date(DD MM YYYY), descr) should present")
}
artcl.ID, _ = strconv.ParseInt(string(lines[0]), 10, 64)
artcl.Title = string(lines[1])
date, err := time.Parse("02 01 2006", string(lines[2]))
if err != nil {
return err
}
artcl.Date = date
artcl.Description = string(lines[3])
return nil
}