Skip to content

Commit

Permalink
implement commonmeta reader
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Apr 26, 2024
1 parent 5e5a926 commit 037e19e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Commonmeta reads and/or writes these metadata formats:

| Format | Name | Content Type | Read | Write |
| ------------------------------------------------------------------------------------------------ | ------------- | -------------------------------------- | ------- | ------- |
| [Commonmeta](https://docs.commonmeta.org) | commonmeta | application/vnd.commonmeta+json | later | yes |
| [Commonmeta](https://docs.commonmeta.org) | commonmeta | application/vnd.commonmeta+json | yes | yes |
| [CrossRef XML](https://www.crossref.org/schema/documentation/unixref1.1/unixref1.1.html) | crossrefxml | application/vnd.crossref.unixref+xml | later | later |
| [Crossref](https://api.crossref.org) | crossref | application/vnd.crossref+json | yes | n/a |
| [DataCite](https://api.datacite.org/) | datacite | application/vnd.datacite.datacite+json | yes | later |
Expand Down
6 changes: 4 additions & 2 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ commonmeta 10.5555/12345678`,
return
}
} else if str != "" {
if from == "crossref" {
if from == "commonmeta" {
data, err = commonmeta.Load(str)
} else if from == "crossref" {
data, err = crossref.Load(str)
} else if from == "datacite" {
data, err = datacite.Load(str)
Expand All @@ -99,6 +101,6 @@ commonmeta 10.5555/12345678`,
func init() {
rootCmd.AddCommand(convertCmd)

convertCmd.PersistentFlags().StringP("from", "f", "", "the format to convert from")
convertCmd.PersistentFlags().StringP("from", "f", "commonmeta", "the format to convert from")
convertCmd.PersistentFlags().StringP("to", "t", "commonmeta", "the format to convert to")
}
10 changes: 0 additions & 10 deletions cmd/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,4 @@ to quickly create a Cobra application.`,

func init() {
rootCmd.AddCommand(decodeCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// decodeCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// decodeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
10 changes: 0 additions & 10 deletions cmd/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,4 @@ to quickly create a Cobra application.`,

func init() {
rootCmd.AddCommand(encodeCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// encodeCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// encodeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
12 changes: 1 addition & 11 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,10 @@ var versionCmd = &cobra.Command{
Short: "Print the version number of commonmeta",
Long: `All software has versions. This is commonmeta's`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Commonmeta v0.2.2 -- HEAD")
fmt.Println("Commonmeta v0.2.3 -- HEAD")
},
}

func init() {
rootCmd.AddCommand(versionCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
25 changes: 25 additions & 0 deletions commonmeta/commonmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ package commonmeta
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path"

"github.com/front-matter/commonmeta/schemautils"

Expand Down Expand Up @@ -232,6 +235,28 @@ type Title struct {
Language string `json:"language,omitempty"`
}

// Load loads the metadata for a single work from a JSON file
func Load(filename string) (Data, error) {
var data Data

extension := path.Ext(filename)
if extension != ".json" {
return data, errors.New("invalid file extension")
}
file, err := os.Open(filename)
if err != nil {
return data, errors.New("error reading file")
}
defer file.Close()

decoder := json.NewDecoder(file)
err = decoder.Decode(&data)
if err != nil {
return data, err
}
return data, nil
}

// Read reads commonmeta metadata.
func Read(content Data) (Data, error) {
data := content
Expand Down
27 changes: 9 additions & 18 deletions schemautils/schemas/commonmeta_v0.13.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"properties": {
"organization": { "$ref": "#/definitions/organization" }
}
},
"uniqueItems": true
}
},
"contributorRole": {
"description": "The type of contribution made by a contributor",
Expand Down Expand Up @@ -192,8 +191,7 @@
"Internet Archive",
"DWT"
]
},
"uniqueItems": true
}
},
"container": {
"description": "The container of the resource.",
Expand Down Expand Up @@ -257,13 +255,11 @@
"items": {
"$ref": "#/definitions/contributorRole"
},
"type": "array",
"uniqueItems": true
"type": "array"
}
}
},
"minItems": 1,
"uniqueItems": true
"minItems": 1
},
"date": {
"description": "The dates for the resource.",
Expand Down Expand Up @@ -342,8 +338,7 @@
},
"required": ["url"]
},
"minItems": 1,
"uniqueItems": true
"minItems": 1
},
"fundingReferences": {
"description": "The funding references for the resource.",
Expand Down Expand Up @@ -488,8 +483,7 @@
},
"required": ["id", "type"]
},
"minItems": 1,
"uniqueItems": true
"minItems": 1
},
"references": {
"type": "array",
Expand All @@ -512,8 +506,7 @@
"unstructured": { "type": "string" }
},
"required": ["key"]
},
"uniqueItems": true
}
},
"subjects": {
"type": "array",
Expand All @@ -527,8 +520,7 @@
}
},
"required": ["subject"]
},
"uniqueItems": true
}
},
"titles": {
"description": "The titles of the resource.",
Expand All @@ -551,8 +543,7 @@
}
},
"required": ["title"]
},
"uniqueItems": true
}
},
"url": {
"description": "The URL of the resource.",
Expand Down

0 comments on commit 037e19e

Please sign in to comment.