package vcard
import "github.com/emersion/go-vcard"
Package vcard implements the vCard format, defined in RFC 6350.
Index ¶
- Constants
- func ToV4(card Card)
- type Address
- type Card
- func (c Card) Add(k string, f *Field)
- func (c Card) AddAddress(address *Address)
- func (c Card) AddName(name *Name)
- func (c Card) AddValue(k, v string)
- func (c Card) Address() *Address
- func (c Card) Addresses() []*Address
- func (c Card) Categories() []string
- func (c Card) FormattedNames() []*Field
- func (c Card) Gender() (sex Sex, identity string)
- func (c Card) Get(k string) *Field
- func (c Card) Kind() Kind
- func (c Card) Name() *Name
- func (c Card) Names() []*Name
- func (c Card) Preferred(k string) *Field
- func (c Card) PreferredValue(k string) string
- func (c Card) Revision() (time.Time, error)
- func (c Card) Set(k string, f *Field)
- func (c Card) SetAddress(address *Address)
- func (c Card) SetCategories(categories []string)
- func (c Card) SetGender(sex Sex, identity string)
- func (c Card) SetKind(kind Kind)
- func (c Card) SetName(name *Name)
- func (c Card) SetRevision(t time.Time)
- func (c Card) SetValue(k, v string)
- func (c Card) Value(k string) string
- func (c Card) Values(k string) []string
- type Decoder
- type Encoder
- type Field
- type Kind
- type Name
- type Params
- func (p Params) Add(k, v string)
- func (p Params) Get(k string) string
- func (p Params) HasType(t string) bool
- func (p Params) Set(k, v string)
- func (p Params) Types() []string
- type Sex
Examples ¶
Constants ¶
const ( MIMEType = "text/vcard" Extension = "vcf" )
MIME type and file extension for VCard, defined in RFC 6350 section 10.1.
const ( ParamLanguage = "LANGUAGE" ParamValue = "VALUE" ParamPreferred = "PREF" ParamAltID = "ALTID" ParamPID = "PID" ParamType = "TYPE" ParamMediaType = "MEDIATYPE" ParamCalendarScale = "CALSCALE" ParamSortAs = "SORT-AS" ParamGeolocation = "GEO" ParamTimezone = "TZ" )
Card property parameters.
const ( // General Properties FieldSource = "SOURCE" FieldKind = "KIND" FieldXML = "XML" // Identification Properties FieldFormattedName = "FN" FieldName = "N" FieldNickname = "NICKNAME" FieldPhoto = "PHOTO" FieldBirthday = "BDAY" FieldAnniversary = "ANNIVERSARY" FieldGender = "GENDER" // Delivery Addressing Properties FieldAddress = "ADR" // Communications Properties FieldTelephone = "TEL" FieldEmail = "EMAIL" FieldIMPP = "IMPP" // Instant Messaging and Presence Protocol FieldLanguage = "LANG" // Geographical Properties FieldTimezone = "TZ" FieldGeolocation = "GEO" // Organizational Properties FieldTitle = "TITLE" FieldRole = "ROLE" FieldLogo = "LOGO" FieldOrganization = "ORG" FieldMember = "MEMBER" FieldRelated = "RELATED" // Explanatory Properties FieldCategories = "CATEGORIES" FieldNote = "NOTE" FieldProductID = "PRODID" FieldRevision = "REV" FieldSound = "SOUND" FieldUID = "UID" FieldClientPIDMap = "CLIENTPIDMAP" FieldURL = "URL" FieldVersion = "VERSION" // Security Properties FieldKey = "KEY" // Calendar Properties FieldFreeOrBusyURL = "FBURL" FieldCalendarAddressURI = "CALADRURI" FieldCalendarURI = "CALURI" )
Card properties.
const ( // Generic TypeHome = "home" TypeWork = "work" // For FieldTelephone TypeText = "text" TypeVoice = "voice" // Default TypeFax = "fax" TypeCell = "cell" TypeVideo = "video" TypePager = "pager" TypeTextPhone = "textphone" // For FieldRelated TypeContact = "contact" TypeAcquaintance = "acquaintance" TypeFriend = "friend" TypeMet = "met" TypeCoWorker = "co-worker" TypeColleague = "colleague" TypeCoResident = "co-resident" TypeNeighbor = "neighbor" TypeChild = "child" TypeParent = "parent" TypeSibling = "sibling" TypeSpouse = "spouse" TypeKin = "kin" TypeMuse = "muse" TypeCrush = "crush" TypeDate = "date" TypeSweetheart = "sweetheart" TypeMe = "me" TypeAgent = "agent" TypeEmergency = "emergency" )
Values for ParamType.
Functions ¶
func ToV4 ¶
func ToV4(card Card)
ToV4 converts a card to vCard version 4.
Types ¶
type Address ¶
type Address struct { *Field PostOfficeBox string ExtendedAddress string // e.g., apartment or suite number StreetAddress string Locality string // e.g., city Region string // e.g., state or province PostalCode string Country string }
An Address is a delivery address.
type Card ¶
A Card is an address book entry.
func (Card) Add ¶
Add adds the k, f pair to the list of fields. It appends to any existing fields.
func (Card) AddAddress ¶
AddAddress adds an address to the list of addresses.
func (Card) AddName ¶
AddName adds the specified name to the list of names.
func (Card) AddValue ¶
AddValue adds the k, v pair to the list of field values. It appends to any existing values.
func (Card) Address ¶
Address returns the preferred address of the card. If it isn't specified, it returns nil.
func (Card) Addresses ¶
Addresses returns addresses of the card.
func (Card) Categories ¶
Categories returns category information about the card, also known as "tags".
func (Card) FormattedNames ¶
FormattedNames returns formatted names of the card. The length of the result is always greater or equal to 1.
func (Card) Gender ¶
Gender returns this card's gender.
func (Card) Get ¶
Get returns the first field of the card for the given property. If there is no such field, it returns nil.
func (Card) Kind ¶
Kind returns the kind of the object represented by this card. If it isn't specified, it returns the default: KindIndividual.
func (Card) Name ¶
Name returns the preferred name of the card. If it isn't specified, it returns nil.
func (Card) Names ¶
Names returns names of the card.
func (Card) Preferred ¶
Preferred returns the preferred field of the card for the given property.
func (Card) PreferredValue ¶
PreferredValue returns the preferred field value of the card.
func (Card) Revision ¶
Revision returns revision information about the current card.
func (Card) Set ¶
Set sets the key k to the single field f. It replaces any existing field.
func (Card) SetAddress ¶
SetAddress replaces the list of addresses with the single specified address.
func (Card) SetCategories ¶
SetCategories sets category information about the card.
func (Card) SetGender ¶
SetGender sets this card's gender.
func (Card) SetKind ¶
SetKind sets the kind of the object represented by this card.
func (Card) SetName ¶
SetName replaces the list of names with the single specified name.
func (Card) SetRevision ¶
SetRevision sets revision information about the current card.
func (Card) SetValue ¶
SetValue sets the field k to the single value v. It replaces any existing value.
func (Card) Value ¶
Value returns the first field value of the card for the given property. If there is no such field, it returns an empty string.
func (Card) Values ¶
Values returns a list of values for a given property.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder parses cards.
func NewDecoder ¶
NewDecoder creates a new Decoder reading cards from an io.Reader.
Code:
Example¶
{
f, err := os.Open("cards.vcf")
if err != nil {
log.Fatal(err)
}
defer f.Close()
dec := vcard.NewDecoder(f)
for {
card, err := dec.Decode()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
log.Println(card.PreferredValue(vcard.FieldFormattedName))
}
}
func (*Decoder) Decode ¶
Decode parses a single card.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder formats cards.
func NewEncoder ¶
NewEncoder creates a new Encoder that writes cards to w.
Code:
Example¶
{
destFile, err := os.Create("cards.vcf")
if err != nil {
log.Fatal(err)
}
defer destFile.Close()
// data in order: first name, middle name, last name, telephone number
contacts := [][4]string{
{"John", "Webber", "Maxwell", "(+1) 199 8714"},
{"Donald", "", "Ron", "(+44) 421 8913"},
{"Eric", "E.", "Peter", "(+37) 221 9903"},
{"Nelson", "D.", "Patrick", "(+1) 122 8810"},
}
var (
// card is a map of strings to []*vcard.Field objects
card = make(vcard.Card)
// destination where the vcard will be encoded to
enc = vcard.NewEncoder(destFile)
)
for _, entry := range contacts {
// set only the value of a field by using card.SetValue.
// This does not set parameters
card.SetValue(vcard.FieldFormattedName, strings.Join(entry[:3], " "))
card.SetValue(vcard.FieldTelephone, entry[3])
// set the value of a field and other parameters by using card.Set
card.Set(vcard.FieldName, &vcard.Field{
Value: strings.Join(entry[:3], ";"),
Params: map[string][]string{
vcard.ParamSortAs: []string{
entry[0] + " " + entry[2],
},
},
})
// make the vCard version 4 compliant
vcard.ToV4(card)
err := enc.Encode(card)
if err != nil {
log.Fatal(err)
}
}
}
func (*Encoder) Encode ¶
Encode formats a card. The card must have a FieldVersion field.
type Field ¶
A field contains a value and some parameters.
type Kind ¶
type Kind string
Kind is an object's kind.
const ( KindIndividual Kind = "individual" KindGroup Kind = "group" KindOrganization Kind = "org" KindLocation Kind = "location" )
Values for FieldKind.
type Name ¶
type Name struct { *Field FamilyName string GivenName string AdditionalName string HonorificPrefix string HonorificSuffix string }
Name contains an object's name components.
type Params ¶
Params is a set of field parameters.
func (Params) Add ¶
Add adds the k, v pair to the list of parameters. It appends to any existing values.
func (Params) Get ¶
Get returns the first value with the key k. It returns an empty string if there is no such value.
func (Params) HasType ¶
HasType returns true if and only if the field have the provided type.
func (Params) Set ¶
Set sets the parameter k to the single value v. It replaces any existing value.
func (Params) Types ¶
Types returns the field types.
type Sex ¶
type Sex string
Sex is an object's biological sex.
const ( SexUnspecified Sex = "" SexFemale Sex = "F" SexMale Sex = "M" SexOther Sex = "O" SexNone Sex = "N" SexUnknown Sex = "U" )
Source Files ¶
card.go decoder.go encoder.go v4.go
- Version
- v0.0.0-20241024213814-c9703dde27ff (latest)
- Published
- Oct 24, 2024
- Platform
- linux/amd64
- Imports
- 7 packages
- Last checked
- 2 months ago –
Tools for package owners.