Skip to content

Commit

Permalink
Added missing types and their validation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-lindner committed May 7, 2022
1 parent 9bc06c0 commit 90de0c8
Show file tree
Hide file tree
Showing 11 changed files with 1,058 additions and 54 deletions.
8 changes: 8 additions & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ authors:
- orcid: 'https://orcid.org/0000-0002-1464-3491'
given-names: Alexander
family-names: Lindner
country: DE
identifiers:
- type: url
value: 'https://github.com/alexander-lindner/go-cff'
Expand All @@ -26,3 +27,10 @@ keywords:
license: MPL-2.0
version: 1.0.0
date-released: '2022-04-30'

references:
- doi: '10.5281/zenodo.1234'
languages:
- en
- de
title: 'A test reference'
28 changes: 28 additions & 0 deletions ParserIsbn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cff

import (
"regexp"
)

var regexISBN *regexp.Regexp

func init() {
regexISBN = regexp.MustCompile(`^[0-9\- ]{10,17}X?$`)
}

//UnmarshalYAML parses a cff string into an ISBN object
func (j *ISBN) UnmarshalYAML(unmarshal func(interface{}) error) error {
return CommonUnmarshalYAML(
func(cffString string) {
*j = ISBN(cffString)
},
regexISBN,
unmarshal,
"ISBN",
)
}

//MarshalYAML serializes an ISBN object into a cff string
func (j ISBN) MarshalYAML() (interface{}, error) {
return CommonMarshalYAML(string(j), regexISBN, "ISBN")
}
28 changes: 28 additions & 0 deletions ParserIssn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cff

import (
"regexp"
)

var regexISSN *regexp.Regexp

func init() {
regexISSN = regexp.MustCompile(`^\d{4}-\d{3}[\dxX]$`)
}

//UnmarshalYAML parses a cff string into an ISSN object
func (j *ISSN) UnmarshalYAML(unmarshal func(interface{}) error) error {
return CommonUnmarshalYAML(
func(cffString string) {
*j = ISSN(cffString)
},
regexISSN,
unmarshal,
"ISSN",
)
}

//MarshalYAML serializes an ISSN object into a cff string
func (j ISSN) MarshalYAML() (interface{}, error) {
return CommonMarshalYAML(string(j), regexISSN, "ISSN")
}
28 changes: 28 additions & 0 deletions ParserLanguage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cff

import (
"regexp"
)

var regexLanguage *regexp.Regexp

func init() {
regexLanguage = regexp.MustCompile(`^[a-z]{2,3}$`)
}

//UnmarshalYAML parses a cff string into a Language object
func (j *Language) UnmarshalYAML(unmarshal func(interface{}) error) error {
return CommonUnmarshalYAML(
func(cffString string) {
*j = Language(cffString)
},
regexLanguage,
unmarshal,
"language",
)
}

//MarshalYAML serializes a Language object into a cff string
func (j Language) MarshalYAML() (interface{}, error) {
return CommonMarshalYAML(string(j), regexLanguage, "language")
}
28 changes: 28 additions & 0 deletions ParserOrcid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cff

import (
"regexp"
)

var regexORCID *regexp.Regexp

func init() {
regexORCID = regexp.MustCompile(`https://orcid\.org/[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]{1}`)
}

//UnmarshalYAML parses a cff string into an ORCID object
func (j *ORCID) UnmarshalYAML(unmarshal func(interface{}) error) error {
return CommonUnmarshalYAML(
func(cffString string) {
*j = ORCID(cffString)
},
regexORCID,
unmarshal,
"ORCID",
)
}

//MarshalYAML serializes an ORCID object into a cff string
func (j ORCID) MarshalYAML() (interface{}, error) {
return CommonMarshalYAML(string(j), regexORCID, "ORCID")
}
28 changes: 28 additions & 0 deletions ParserPmcid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cff

import (
"regexp"
)

var regexPMCID *regexp.Regexp

func init() {
regexPMCID = regexp.MustCompile(`^PMC[0-9]{7}$`)
}

//UnmarshalYAML parses a cff string into a PMCID object
func (j *PMCID) UnmarshalYAML(unmarshal func(interface{}) error) error {
return CommonUnmarshalYAML(
func(cffString string) {
*j = PMCID(cffString)
},
regexPMCID,
unmarshal,
"PMC ID",
)
}

//MarshalYAML serializes a PMCID object into a cff string
func (j PMCID) MarshalYAML() (interface{}, error) {
return CommonMarshalYAML(string(j), regexPMCID, "PMC ID")
}
28 changes: 28 additions & 0 deletions ParserSwh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cff

import (
"regexp"
)

var regexSWHID *regexp.Regexp

func init() {
regexSWHID = regexp.MustCompile(`^swh:1:(snp|rel|rev|dir|cnt):[0-9a-fA-F]{40}$`)
}

//UnmarshalYAML parses a cff string into a SWHID object
func (j *SWHID) UnmarshalYAML(unmarshal func(interface{}) error) error {
return CommonUnmarshalYAML(
func(cffString string) {
*j = SWHID(cffString)
},
regexSWHID,
unmarshal,
"SWH Identifier",
)
}

//MarshalYAML serializes a SWHID object into a cff string
func (j SWHID) MarshalYAML() (interface{}, error) {
return CommonMarshalYAML(string(j), regexSWHID, "SWH Identifier")
}
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ See [Documentation](https://alexander-lindner.github.io/go-cff/) for more inform

## ToDo's

* definitions.country
* definitions.email
* definitions.fax
* definitions.license-enum
* definitions.orcid
* definitions.reference.isbn
* definitions.reference.issn
* definitions.reference.languages
* definitions.reference.pmcid
* definitions.reference.status
* definitions.reference.type
* definitions.swh-identifier
* definitions.url
* definitions.version
* ~~definitions.country~~
* ~~definitions.email~~
* ~~definitions.fax~~
* ~~definitions.license-enum~~
* ~~definitions.orcid~~
* ~~definitions.reference.isbn~~
* ~~definitions.reference.issn~~
* ~~definitions.reference.languages~~
* ~~definitions.reference.pmcid~~
* ~~definitions.reference.status~~
* ~~definitions.reference.type~~
* ~~definitions.swh-identifier~~
* ~~definitions.url~~
* ~~definitions.version~~
30 changes: 30 additions & 0 deletions Utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cff

import (
"errors"
"regexp"
)

func CommonMarshalYAML(cffString string, regexPattern *regexp.Regexp, typeOfString string) (interface{}, error) {
var err error
if regexPattern.MatchString(cffString) {
return cffString, err
} else {
return "", errors.New("invalid " + typeOfString + ": " + cffString)
}
}

func CommonUnmarshalYAML(matchAction func(string), regexPattern *regexp.Regexp, unmarshal func(interface{}) error, typeOfString string) error {
var cffString string
err := unmarshal(&cffString)
if err != nil {
return err
}

if regexPattern.MatchString(cffString) {
matchAction(cffString)
} else {
err = errors.New("invalid " + typeOfString + ": " + cffString)
}
return err
}
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ func main() {
Version: "sd",
}
file = "./CITATION2.cff"
_ := cff.SaveFile(file, content)
cff.SaveFile(file, content)
}
```
Loading

0 comments on commit 90de0c8

Please sign in to comment.