#
Parse
go-cff
provides two ways to parse the data:
cff.Parse(content string)
for parsing a stringcff.ParseFile(filename string)
for parsing a file
The returned value is a cff.CFF
object.
This object contains all the data from the CFF file, not available fields are empty.
It is recommended to refer to that documentation for the available fields and their children or to use an intelligent IDE.
#
Example
parsedCFF, err := cff.ParseFile("./CITATION.cff")
if err != nil {
fmt.Println("Error: ", err)
return
}
fmt.Println("CFF File Version: ", parsedCFF.CffVersion)
#
with arrays
parsedCFF, err := cff.ParseFile("./CITATION.cff")
if err != nil {
fmt.Println("Error: ", err)
return
}
for _, reference := range parsedCFF.References {
fmt.Println(reference.Title)
}
#
The PersonEntity
type
The CFF file format allows in several places two types at the same time:
Go doesn't allow to have a variable with several types.
Thus, we created a small proxy struct to handle this situation: PersonEntity
.
type PersonEntity struct {
Person Person
Entity Entity
IsEntity bool
IsPerson bool
}
As you see, it has the two fields for the Person
and the Entity
type.
Additionally, it also has two boolean fields to know if it is a Person
or an Entity
.
The leftover field contains an empty struct of the corresponding type.
#
Example
parser, _ := cff.ParseFile("./CITATION.cff")
for _, item := range parser.Authors {
if item.IsPerson {
fmt.Println(item.Person.GivenNames)
} else {
fmt.Println(item.Entity.Address)
}
}
#
The Identifier
type
Equal to the PersonEntity
Identifier
type is a proxy struct.
type Identifier struct {
DOI IdentifierDOI
URL IdentifierURL
SWH IdentifierSWH
Other IdentifierOther
IsDOI bool
IsURL bool
IsSWH bool
IsOther bool
}
Therefore, use the IsDOI
, IsURL
, IsSWH
and IsOther
fields to know if the identifier is from type DOI, URL, SWH or Other.
#
Example
document, _ := cff.ParseFile("./CITATION.cff")
for _, item := range parser.Identifiers {
if item.IsURL {
fmt.Println(item.URL.Value)
}
}