From 61b67d5a54974ba0ecb9cfb9dd4d483ab0b179f3 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Wed, 8 May 2024 18:17:05 +0200 Subject: [PATCH] rename some variables --- cmd/list.go | 10 +++++----- cmd/sample.go | 4 ++-- commonmeta/reader.go | 13 +++++++++++++ commonmeta/writer.go | 13 +++++++++++++ crossref/reader.go | 26 +++++++++++++++++++------- crossref/reader_test.go | 8 ++++---- crossrefxml/reader.go | 4 ++-- datacite/reader.go | 14 +++++++------- 8 files changed, 65 insertions(+), 27 deletions(-) diff --git a/cmd/list.go b/cmd/list.go index 4306209..5360662 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -67,15 +67,15 @@ var listCmd = &cobra.Command{ } if str != "" && from == "crossref" { - data, err = crossref.LoadList(str) + data, err = crossref.LoadAll(str) } else if str != "" && from == "crossrefxml" { - data, err = crossrefxml.LoadList(str) + data, err = crossrefxml.LoadAll(str) } else if str != "" && from == "datacite" { - data, err = datacite.LoadList(str) + data, err = datacite.LoadAll(str) } else if from == "crossref" { - data, err = crossref.FetchList(number, member, type_, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive) + data, err = crossref.FetchAll(number, member, type_, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive) } else if from == "datacite" { - data, err = datacite.FetchList(number, sample) + data, err = datacite.FetchAll(number, sample) } if err != nil { fmt.Println(err) diff --git a/cmd/sample.go b/cmd/sample.go index 76186c3..be6af92 100644 --- a/cmd/sample.go +++ b/cmd/sample.go @@ -50,9 +50,9 @@ var sampleCmd = &cobra.Command{ var err error sample := true if from == "crossref" { - data, err = crossref.FetchList(number, member, type_, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive) + data, err = crossref.FetchAll(number, member, type_, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive) } else if from == "datacite" { - data, err = datacite.FetchList(number, sample) + data, err = datacite.FetchAll(number, sample) } if err != nil { fmt.Println(err) diff --git a/commonmeta/reader.go b/commonmeta/reader.go index 12484fa..5d6dbbd 100644 --- a/commonmeta/reader.go +++ b/commonmeta/reader.go @@ -2,12 +2,25 @@ package commonmeta import ( + "bufio" "encoding/json" "errors" + "io" "os" "path" ) +type Reader struct { + r *bufio.Reader +} + +// NewReader returns a new Reader that reads from r. +func NewReader(r io.Reader) *Reader { + return &Reader{ + r: bufio.NewReader(r), + } +} + // ContributorRoles list of contributor roles defined in commonmeta schema. // // from commonmeta schema diff --git a/commonmeta/writer.go b/commonmeta/writer.go index 11aaad9..d4667fc 100644 --- a/commonmeta/writer.go +++ b/commonmeta/writer.go @@ -1,13 +1,26 @@ package commonmeta import ( + "bufio" "encoding/json" "fmt" + "io" "github.com/front-matter/commonmeta/schemautils" "github.com/xeipuuv/gojsonschema" ) +type Writer struct { + w *bufio.Writer +} + +// NewWriter returns a new Writer that writes to w. +func NewWriter(w io.Writer) *Writer { + return &Writer{ + w: bufio.NewWriter(w), + } +} + // Write writes commonmeta metadata. func Write(data Data) ([]byte, []gojsonschema.ResultError) { output, err := json.Marshal(data) diff --git a/crossref/reader.go b/crossref/reader.go index 4023c02..90b4981 100644 --- a/crossref/reader.go +++ b/crossref/reader.go @@ -2,6 +2,7 @@ package crossref import ( + "bufio" "encoding/json" "errors" "fmt" @@ -24,6 +25,17 @@ import ( "github.com/front-matter/commonmeta/utils" ) +type Reader struct { + r *bufio.Reader +} + +// NewReader returns a new Reader that reads from r. +func NewReader(r io.Reader) *Reader { + return &Reader{ + r: bufio.NewReader(r), + } +} + // Content is the struct for the message in tge JSON response from the Crossref API type Content struct { ID string `json:"id"` @@ -255,11 +267,11 @@ func Fetch(str string) (commonmeta.Data, error) { return data, nil } -// FetchList gets the metadata for a list of works from the Crossref API and converts it to the Commonmeta format -func FetchList(number int, member string, _type string, sample bool, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]commonmeta.Data, error) { +// FetchAll gets the metadata for a list of works from the Crossref API and converts it to the Commonmeta format +func FetchAll(number int, member string, _type string, sample bool, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]commonmeta.Data, error) { var data []commonmeta.Data - content, err := GetList(number, member, _type, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive) + content, err := GetAll(number, member, _type, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive) if err != nil { return data, err } @@ -317,8 +329,8 @@ func Get(pid string) (Content, error) { return response.Message, err } -// GetList gets the metadata for a list of works from the Crossref API -func GetList(number int, member string, _type string, sample bool, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]Content, error) { +// GetAll gets the metadata for a list of works from the Crossref API +func GetAll(number int, member string, _type string, sample bool, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]Content, error) { // the envelope for the JSON response from the Crossref API type Response struct { Status string `json:"status"` @@ -392,8 +404,8 @@ func Load(filename string) (commonmeta.Data, error) { return data, nil } -// LoadList loads the metadata for a list of works from a JSON file and converts it to the Commonmeta format -func LoadList(filename string) ([]commonmeta.Data, error) { +// LoadAll loads the metadata for a list of works from a JSON file and converts it to the Commonmeta format +func LoadAll(filename string) ([]commonmeta.Data, error) { var data []commonmeta.Data var content []Content var err error diff --git a/crossref/reader_test.go b/crossref/reader_test.go index 49bd9af..7886e8c 100644 --- a/crossref/reader_test.go +++ b/crossref/reader_test.go @@ -127,7 +127,7 @@ func ExampleQueryURL() { // https://api.crossref.org/works?filter=member%3A340%2Ctype%3Ajournal-article&order=desc&rows=10&sort=published } -func TestGetList(t *testing.T) { +func TestGetAll(t *testing.T) { t.Parallel() type testCase struct { @@ -143,12 +143,12 @@ func TestGetList(t *testing.T) { {number: 2, member: "", _type: "", sample: true}, } for _, tc := range testCases { - got, err := crossref.GetList(tc.number, tc.member, tc._type, true, false, false, false, false, false, false, false, false) + got, err := crossref.GetAll(tc.number, tc.member, tc._type, true, false, false, false, false, false, false, false, false) if err != nil { - t.Errorf("GetList (%v): error %v", tc.number, err) + t.Errorf("GetAll (%v): error %v", tc.number, err) } if diff := cmp.Diff(tc.number, len(got)); diff != "" { - t.Errorf("GetList mismatch (-want +got):\n%s", diff) + t.Errorf("GetAll mismatch (-want +got):\n%s", diff) } } } diff --git a/crossrefxml/reader.go b/crossrefxml/reader.go index cf7d936..e21dd2b 100644 --- a/crossrefxml/reader.go +++ b/crossrefxml/reader.go @@ -1180,8 +1180,8 @@ func Load(filename string) (commonmeta.Data, error) { return data, nil } -// LoadList loads the metadata for a list of works from an XML file and converts it to the Commonmeta format -func LoadList(filename string) ([]commonmeta.Data, error) { +// LoadAll loads the metadata for a list of works from an XML file and converts it to the Commonmeta format +func LoadAll(filename string) ([]commonmeta.Data, error) { type Response struct { ListRecords struct { Record []struct { diff --git a/datacite/reader.go b/datacite/reader.go index fa3a824..58ca84b 100644 --- a/datacite/reader.go +++ b/datacite/reader.go @@ -274,10 +274,10 @@ func Fetch(str string) (commonmeta.Data, error) { return data, nil } -// FetchList gets the metadata for a list of works from the DataCite API and returns Commonmeta metadata. -func FetchList(number int, sample bool) ([]commonmeta.Data, error) { +// FetchAll gets the metadata for a list of works from the DataCite API and returns Commonmeta metadata. +func FetchAll(number int, sample bool) ([]commonmeta.Data, error) { var data []commonmeta.Data - content, err := GetList(number, sample) + content, err := GetAll(number, sample) if err != nil { return data, err } @@ -306,8 +306,8 @@ func Load(filename string) (commonmeta.Data, error) { return data, nil } -// LoadList loads a list of DataCite metadata from a JSON string and returns Commonmeta metadata. -func LoadList(filename string) ([]commonmeta.Data, error) { +// LoadAll loads a list of DataCite metadata from a JSON string and returns Commonmeta metadata. +func LoadAll(filename string) ([]commonmeta.Data, error) { var data []commonmeta.Data response, err := ReadJSONLines(filename) @@ -717,8 +717,8 @@ func GetContributor(v ContentContributor) commonmeta.Contributor { } } -// GetList gets the metadata for a list of works from the DataCite API -func GetList(number int, sample bool) ([]Content, error) { +// GetAll gets the metadata for a list of works from the DataCite API +func GetAll(number int, sample bool) ([]Content, error) { // the envelope for the JSON response from the DataCite API type Response struct { Data []Content `json:"data"`