// Package note holds the rules for a note: what a valid note is. It knows // nothing about files or the web. package note import ( "errors" "fmt" "strings" "time" "unicode/utf8" ) // MaxLength is the most characters a note can have. const MaxLength = 200 // Note is one note. The JSON tags give the names used in the file and by the // API. type Note struct { ID int `json:"id"` Text string `json:"text"` Created time.Time `json:"created"` } // New trims and checks the text, and returns a note without an ID. The store // gives it an ID when it is added. The errors are meant for people to read. func New(text string, created time.Time) (Note, error) { text = strings.TrimSpace(text) if text == "" { return Note{}, errors.New("the note is empty") } if utf8.RuneCountInString(text) > MaxLength { return Note{}, fmt.Errorf("the note can have at most %d characters", MaxLength) } return Note{Text: text, Created: created}, nil }