maxlath / wikibase-cli

read and edit a Wikibase instance from the command line
MIT License
226 stars 24 forks source link

generate-template does not simplify references #76

Closed simon04 closed 4 years ago

simon04 commented 5 years ago

Running wd generate-template Q40 -p claims.P1082 generates an object without simplifying the references. Thus, wd create-item fails with invalid property [ 'snaks' ] when trying to re-use references.

Fragment of the generate-template output:

        "references": [
          {
            "snaks": {
              "P854": [
                {
                  "value": "https://www.statistik.at/web_de/statistiken/bevoelkerung/bevoelkerungsstand_und_veraenderung/bevoelkerung_zu_jahres-_quartalsanfang/023582.html",
                  "hash": "95743deb1973e67903f4834ee4cf600c8cb7c017"
                }
              ],
              "P123": [
                {
                  "value": "Q358870",
                  "hash": "de715942ebd0a0be56fe2c6e745f5e60442043ae"
                }
              ]
            },
            "hash": "0418ac840e0a5a172a1eb7cbbd6c76f7115f576c"
          }
        ],

wd --version = 7.3.0

Test case catching this problem:


  it('should generate get rid of snaks in references', done => {
    execa.shell('./bin/wd gt Q40 -p claims.P1082')
    .then(res => {
      console.log(res.stdout)
      res.stdout.should.not.match(/snaks/)
      done()
    })
    .catch(done)
  })
simon04 commented 5 years ago

Specifying simplifyOptions: {keepHashes: false} and applying the following function to the references gives as useful result:

function flattenReferences(statement) {
  return statement.references.map(reference =>
    Object.keys(reference).reduce((obj, property) => {
      obj[property] = reference[property][0];
      return obj;
    }, {})
  );
maxlath commented 5 years ago

could you retry with the freshly published v7.4.1? it now tries to minimize the template by all possible means, removing the snaks wrapper object among others