vcard package

import "code.pfad.fr/gopim/vcard"

Package vcard implements the vCard format, defined in RFC 6350.

For usage examples refer to New to create a vCard and NewDecoder to read a vCard.

License and credits

Code is available under the MIT license.

Large inspiration has been taken from the emersion/go-vcard package (available under MIT).

Index

Examples

Constants

MIME type and file extension for vCard rfc6350#section-10.1

Card property names.

Parameter names.

type Data

Data represents a vCard

func New

New creates a new vCard 4.0 (a ProductID property is added, if missing)

Example
package main

import (
	"bytes"
	"fmt"
	"log"
	"strings"
	"time"

	"code.pfad.fr/gopim/vcard"
)

func main() {
	names := vcard.Names{}.
		WithFamilyName("Public").
		WithGivenName("John").
		WithAdditionalName("Quinlan", "Paul").
		WithHonorificPrefix("Mr.").
		WithHonorificSuffix("Esq.")
	d := vcard.New(
		vcard.Property{
			Name:  vcard.Property_FormattedName,
			Value: names.FormattedName(),
		},
		names.ToProperty(),
		vcard.Revision(time.Date(1996, 10, 22, 14, 00, 00, 0, time.UTC)),
		vcard.Property{
			Name:  vcard.Property_UID,
			Value: vcard.NewPropertyTexts("123"),
		},
		vcard.OrganizationLevels{"ABC, Inc.", "North American Division"}.ToProperty(),
		vcard.Property{
			Name:  vcard.Property_Email,
			Value: vcard.NewPropertyTexts("jqpublic@xyz.example.com"),
		}.WithType(vcard.ParameterType_Work),
		vcard.Property{
			Name:  vcard.Property_Telephone,
			Value: vcard.NewPropertyTexts("tel:+1-418-656-9254;ext=102"),
		}.WithValue("uri").WithType(vcard.ParameterType_Work, vcard.ParameterType_Voice).WithPreference(1),
		vcard.Property{
			Name:  vcard.Property_Telephone,
			Value: vcard.NewPropertyTexts("tel:+1-418-262-6501"),
		}.WithValue("uri").WithType(vcard.ParameterType_Work, vcard.ParameterType_Cell, vcard.ParameterType_Voice, vcard.ParameterType_Video, vcard.ParameterType_Text),
	)
	var buf bytes.Buffer
	err := vcard.NewEncoder(&buf).Encode(d)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(strings.ReplaceAll(buf.String(), "\r\n", "\n"))
}

Output:

BEGIN:VCARD
VERSION:4.0
FN:Mr. John Q. Public\, Esq.
N:Public;John;Quinlan,Paul;Mr.;Esq.
REV:19961022T140000Z
UID:123
ORG:ABC\, Inc.;North American Division
EMAIL;TYPE=work:jqpublic@xyz.example.com
TEL;VALUE=uri;TYPE="work,voice";PREF=1:tel:+1-418-656-9254;ext=102
TEL;VALUE=uri;TYPE="work,cell,voice,video,text":tel:+1-418-262-6501
PRODID:-//GoPim//code.pfad.fr/gopim/vcard (devel)//EN
END:VCARD

func (Data) Categories

Category information about the vCard, also known as "tags". rfc6350#section-6.7.1

func (Data) Gender

Sex and gender identity. rfc6350#section-6.2.7

func (Data) Kind

Kind of object the vCard represents rfc6350#section-6.1.4

func (Data) Names

Names returns the preferred names. rfc6350#section-6.2.2

func (*Data) PropertyDelete

PropertyDelete removes all properties with the given name

func (Data) PropertyFirst

func (Data) PropertyPreferred

PropertyPreferred returns the property with the highest preference

func (Data) Revision

Revision information about the current vCard. rfc6350#section-6.7.4

func (Data) Validate

func (Data) Version

type Decoder

A Decoder parses vcard.Data

func NewDecoder

NewDecoder creates a new Decoder reading vcard.Data from an io.Reader.

Example
package main

import (
	"fmt"
	"log"
	"os"

	"code.pfad.fr/gopim/vcard"
)

func main() {
	f, err := os.Open("testdata/rfc_example/7.2.4_boss.vcf")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	d, err := vcard.NewDecoder(f).Decode()
	if err != nil {
		log.Fatal(err)
	}
	p, ok := d.PropertyPreferred(vcard.Property_FormattedName)
	fmt.Println(ok, p.Value.SplitTexts()[0])
	p, ok = d.PropertyPreferred(vcard.Property_Telephone)
	fmt.Println(ok, p.Value.SplitTexts()[0])
}

Output:

true J. Doe
true tel:+1-555-555-5555

func (Decoder) Decode

Decode parses a single vcard.Data.

type Encoder

An Encoder encodes Data in the vCard format.

func NewEncoder

NewEncoder creates a new Encoder. See New for a usage example.

func (Encoder) Encode

Encode write a single Data in the vCard format to the underlying io.Writer

type Kind

Kind is a data kind.

func (Kind) IsKnown

func (Kind) ToProperty

type Names

func (Names) FormattedName

FormattedName returns a likely representation of the formatted name (skipping empty elements):

HonorificPrefix[0] GivenName[0] AdditionalName[0][0]. FamilyName[0], HonorificSuffix[0]

func (Names) ToProperty

func (Names) WithAdditionalName

func (Names) WithFamilyName

func (Names) WithGivenName

func (Names) WithHonorificPrefix

func (Names) WithHonorificSuffix

type OrganizationLevels

func (OrganizationLevels) ToProperty

type Parameter

type ParameterType

ParameterType is for Parameter_Type.

Known parameter types

func (ParameterType) IsKnown

type Property

func Revision

func (Property) ParameterValues

ParameterValues returns the parameter values, as stored.

Note that according to rfc6350: Parameter values that are not explicitly defined as being case-sensitive are case-insensitive. (only SORT-AS is defined as case-sensitive).

func (Property) Preference

Preference indicate that the corresponding instance of a property is preferred by the vCard author. Lower values correspond to a higher level of preference, with 1 being most preferred. 101 indicates an error parsing the value, 102 indicates no value

func (Property) Types

func (Property) WithPreference

WithPreference sets the preference between 1 and 100. Lower values correspond to a higher level of preference, with 1 being most preferred.

func (Property) WithType

func (Property) WithValue

type PropertyValue

PropertyValue stores the raw value of a Property.

func NewPropertyTexts

NewPropertyTexts creates a new PropertyValue after escaping each text (backslash, newline, comma) and joining by comma.

func (PropertyValue) Names

func (PropertyValue) OrganizationLevels

func (PropertyValue) SplitTexts

SplitTexts returns the value parts of a Property, split by comma (each part being unescaped). Guaranteed to have a len > 0

func (PropertyValue) String

String returns the raw value

func (PropertyValue) Timestamp

type Sex

Sex is an object's biological sex.

func (Sex) IsKnown

func (Sex) ToProperty

type Version

Source Files

decoder.go encoder.go vcard.go

Forge

https://codeberg.org/pfad.fr/gopim-vcard

git clone
https://codeberg.org/pfad.fr/gopim-vcard.git
git@codeberg.org:pfad.fr/gopim-vcard.git