gnucash

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2025 License: MIT Imports: 10 Imported by: 0

README

This package reads GnuCash XML database file (v2) and converts it to equivalent coin structures. cmd/gc2coin shows how it's meant to be used.

Implementation Notes

  • The encoding/xml package is unable to unmarshal properly namespaced tags https://github.com/golang/go/issues/9519. However it is able to ignore the namespace prefixes if they are removed from the struct tags. Consequently we cannot marshal back into proper gnucash XML.

Documentation

Overview

Package gnucash allows reading GnuCash v2 XML database and converting it to a coin ledger.

Index

Constants

View Source
const TimeStamp = "2006-01-02 15:04:05 -0700"

TimeStamp is GnuCash time format

Variables

View Source
var (
	AccountsByGuid     = map[string]*coin.Account{}
	AccountParentGuids = map[*coin.Account]string{}
)

Functions

func AccountFrom

func AccountFrom(a *Account) *coin.Account

func CommodityFrom

func CommodityFrom(c *Commodity) *coin.Commodity

Types

type Account

type Account struct {
	Guid           string     `xml:"id"`
	Name           string     `xml:"name"`
	Type           string     `xml:"type"`
	CommoditySpace string     `xml:"commodity>space"`
	CommodityId    string     `xml:"commodity>id"`
	CommodityScu   int        `xml:"commodity-scu"`
	NonStandardScu int        `xml:"non-standard-scu,omitempty"`
	ParentGuid     string     `xml:"parent"`
	Code           string     `xml:"code,omitempty"`
	Description    string     `xml:"description,omitempty"`
	Slots          []*KvpSlot `xml:"slots>slot"`
}

type Book

type Book struct {
	Accounts     []*Account     `xml:"account"`
	Commodities  []*Commodity   `xml:"commodity"`
	Prices       []*Price       `xml:"pricedb>price"`
	Transactions []*Transaction `xml:"transaction"`
}

func Load

func Load(fn string) *Book

func (*Book) Resolve

func (book *Book) Resolve()

Resolve relinks all the book objects after unmarshaling from XML

type Commodity

type Commodity struct {
	XMLName     xml.Name   `xml:"commodity"`
	Space       string     `xml:"space"`
	Id          string     `xml:"id"`
	Name        string     `xml:"name,omitempty"`
	Code        string     `xml:"xcode,omitempty"`
	Fraction    int64      `xml:"fraction"`
	QuoteSource string     `xml:"quote_source,omitempty"`
	QuoteTz     string     `xml:"quote_tz,omitempty"`
	Slots       []*KvpSlot `xml:"slots>slot"`
}
Commodity = element gnc:commodity {
  attribute version { "2.0.0" },
  ( ( element cmdty:space { "ISO4217" },    # catégorie (monnaies)
      element cmdty:id { text }    # dénomination
    )
  | ( element cmdty:space { text },
      element cmdty:id { text },
      element cmdty:name { text }?,
      element cmdty:xcode { text }?,
      element cmdty:fraction { text }
    )
  ),
  ( element cmdty:get_quotes { empty },
    element cmdty:quote_source { text }?,
    element cmdty:quote_tz { text | empty }?
  )?,
  element cmdty:slots { KvpSlot+ }?
}

type Gnucash

type Gnucash struct {
	XMLName xml.Name `xml:"gnc-v2"`
	Book    Book     `xml:"book"`
}

Gnucash is the root element of the Gnucash DB

type KvpSlot

type KvpSlot struct {
	XMLName xml.Name `xml:"slot"`
	Key     string   `xml:"key"`
	Value   KvpValue `xml:"value"`
}

type KvpValue

type KvpValue struct {
	XMLName xml.Name `xml:"value"`
	Value   string   `xml:",innerxml"`
	Type    string   `xml:"type,attr"`
}

type Price

type Price struct {
	Guid           string `xml:"id"`
	CommoditySpace string `xml:"commodity>space"`
	CommodityId    string `xml:"commodity>id"`
	CurrencySpace  string `xml:"currency>space"`
	CurrencyId     string `xml:"currency>id"`
	Date           string `xml:"time>date"`
	Source         string `xml:"source,omitempty"`
	ValueFraction  string `xml:"value"`
	Type           string `xml:"type,omitempty"`
}

type Split

type Split struct {
	Guid             string `xml:"id"`
	AccountGuid      string `xml:"account"`
	Memo             string `xml:"memo,omitempty"`
	Action           string `xml:"action,omitempty"`
	ReconciledState  string `xml:"reconciled-state"`
	ReconciledStamp  string `xml:"reconcile-date>date,omitempty"`
	ValueFraction    string `xml:"value"`
	QuantityFraction string `xml:"quantity"`
}

type Transaction

type Transaction struct {
	Guid          string   `xml:"id"`
	CurrencySpace string   `xml:"currency>space"`
	CurrencyId    string   `xml:"currency>id"`
	Num           string   `xml:"num,omitempty"`
	PostedStamp   string   `xml:"date-posted>date"`
	EnteredStamp  string   `xml:"date-entered>date"`
	Description   string   `xml:"description,omitempty"`
	Splits        []*Split `xml:"splits>split"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL