emersion / go-vcard

A Go library to parse and format vCard
MIT License
107 stars 34 forks source link

GetAllFields #9

Closed InDaPond closed 6 years ago

InDaPond commented 6 years ago

I would love to access all Fields, so far you only can Get one(the preferred) value by calling Get. You have access to all the Values, but that might not be sufficient: In my case, I want to have the details on all existing phone Numbers, so whether that phoneNumber is for Work, Home, Cell, etc.

So my simple suggestion:

// GetAllFields returns all fields of the card for the given property. If there is    
// no such field, it returns nil.  
func (c Card) GetAllFields(k string) []*Field {  
    fields := c[k]  
    if len(fields) == 0 {  
        return nil  
    }  
    return fields  
}

and on that you can extract all necessary information. Or is there a better way to extract all phone Numbers with the details on them?

InDaPond commented 6 years ago

okay im too stupid to format the code properly, how does a newline work in there?

emersion commented 6 years ago

Hi,

To get all fields you can just use regular map access:

var card vcard.Card // Let's assume `card` is a vcard
tel := card[vcard.FieldTelephone]
emails, hasEmails := card[vcard.FieldEmail]
InDaPond commented 6 years ago

Thank you for the fast reply - this is indeed what I was looking for, therefore my issue makes no sense - CLOSED :)

InDaPond commented 6 years ago

just a quick follow-up question: If I go for card.PreferredValue(vcard.FieldTelephone)
I get something like this: +1 49 444666 whereas the real number is +49 444666 - Is there a method to remove that +1 or do I just regex that away?

emersion commented 6 years ago

It seems like the original data contains this +1 - vcard doesn't care about this, it just parses the field and returns its value. Read the original vcard file to check this.

Side note, don't use regexp when you have a good stdlib with things like strings.TrimPrefix.

InDaPond commented 6 years ago

Thanks, within all the contacts I took one as a sample which seemed fitting - but you are right again, it's a bug there. Thanks and hopefully finally Closed ;)