summaryrefslogtreecommitdiffstats
path: root/database.go
blob: 57d200892583ad59abe681fb0136d1b8313b8d17 (plain)
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
package bibtex

import (
	"bytes"
	"strconv"
	"strings"
)

type Literal interface{}

type BraceLiteral string
type StringLiteral string
type NumberLiteral int
type VariableLiteral struct {
	Name  string
	Value *Value
}

func Marshal(l Literal) (res string) {
	switch v := l.(type) {
	case BraceLiteral:
		res = "{" + string(v) + "}"
	case StringLiteral:
		res = "\"" + string(v) + "\""
	case NumberLiteral:
		res = strconv.Itoa(int(v))
	case VariableLiteral:
		res = v.Name
	}
	return
}

type Value []Literal

func (v Value) Marshal() string {
	res := make([]string, len(v))
	for i, l := range v {
		res[i] = Marshal(l)
	}
	return strings.Join(res, " # ")
}

func (v Value) String() string {
	var buf bytes.Buffer
	for _, l := range v {
		switch lit := l.(type) {
		case BraceLiteral:
			buf.WriteString(string(lit))
		case StringLiteral:
			buf.WriteString(string(lit))
		case NumberLiteral:
			buf.WriteString(strconv.Itoa(int(lit)))
		case VariableLiteral:
			buf.WriteString(lit.Value.String())
		}
	}
	return buf.String()
}

type Entry struct {
	Type   string
	Key    string
	Fields map[string]Value
	FNames []string
}

type Database struct {
	SNames   []string
	Strings  map[string]Value
	Entries  map[string]Entry
	Preamble Value
	EKeys    []string
}