Skip to content

[DNM] Make the whole data model immutable #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,43 @@ the corresponding serializer, and send back to the API.

## Release notes

### 5.0.0 (dev)

* The whole data model is now immutable:
** Removed `Claim.setMainSnak`
** Removed `Claim.setQualifiers`
** Removed `Entity.setFingerprint`
** Removed `Fingerprint.removeAliases`
** Removed `Fingerprint.removeAliasesFor`
** Removed `Fingerprint.removeDescription`
** Removed `Fingerprint.removeDescriptionFor`
** Removed `Fingerprint.removeLabel`
** Removed `Fingerprint.removeLabelFor`
** Removed `Fingerprint.setAliases`
** Removed `Fingerprint.setDescription`
** Removed `Fingerprint.setLabel`
** Made `Group.setItemContainer` private
** Removed `Item.addSiteLink`
** Removed `Item.addStatement`
** Removed `Item.removeSiteLink`
** Removed `Item.removeStatement`
** Removed `MultiTerm.setLanguageCode`
** Removed `MultiTerm.setTexts`
** Removed `Property.addStatement`
** Removed `Property.removeStatement`
** Removed `SiteLink.setBadges`
** Removed `SnakList.getValidMoveIndices`
** Removed `SnakList.merge`
** Removed `SnakList.move`
** Removed `SnakList.moveDown`
** Removed `SnakList.moveUp`
** Removed `Statement.setClaim`
** Made `Statement.setRank` private
** Removed `Statement.setReferences`
** Removed `Term.setLanguageCode`
** Removed `Term.setText`


### 4.0.0 (2017-10-09)

* Made the library a pure JavaScript library.
Expand Down
40 changes: 12 additions & 28 deletions src/Claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
* @param {wikibase.datamodel.SnakList|null} [qualifiers=new wikibase.datamodel.SnakList()]
* @param {string|null} [guid=null] The Global Unique Identifier of this Claim. Can be null if this
* is a new Claim, not yet stored in the database and associated with some entity.
*
* @throws {Error} if parameter is not a Snak instance.
* @throws {Error} if parameter is not a SnakList instance.
*/
var SELF = wb.datamodel.Claim = function WbDataModelClaim( mainSnak, qualifiers, guid ) {
this.setMainSnak( mainSnak );
this.setQualifiers( qualifiers || new wb.datamodel.SnakList() );
if( !( mainSnak instanceof wb.datamodel.Snak ) ) {
throw new Error( 'Main snak needs to be a Snak instance' );
}
if( qualifiers && !( qualifiers instanceof wb.datamodel.SnakList ) ) {
throw new Error( 'Qualifiers have to be a SnakList object' );
}

this._mainSnak = mainSnak;
this._qualifiers = qualifiers || new wb.datamodel.SnakList();
this._guid = guid || null;
};

Expand Down Expand Up @@ -62,39 +72,13 @@ $.extend( SELF.prototype, {
return this._mainSnak;
},

/**
* Overwrites the current main Snak.
*
* @param {wikibase.datamodel.Snak} mainSnak
*
* @throws {Error} if parameter is not a Snak instance.
*/
setMainSnak: function( mainSnak ) {
if( !( mainSnak instanceof wb.datamodel.Snak ) ) {
throw new Error( 'Main snak needs to be a Snak instance' );
}
this._mainSnak = mainSnak;
},

/**
* @return {wikibase.datamodel.SnakList}
*/
getQualifiers: function() {
return this._qualifiers;
},

/**
* @param {wikibase.datamodel.SnakList} qualifiers
*
* @throws {Error} if parameter is not a SnakList instance.
*/
setQualifiers: function( qualifiers ) {
if( !( qualifiers instanceof wb.datamodel.SnakList ) ) {
throw new Error( 'Qualifiers have to be a SnakList object' );
}
this._qualifiers = qualifiers;
},

/**
* @param {*} claim
* @return {boolean}
Expand Down
7 changes: 0 additions & 7 deletions src/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ $.extend( SELF.prototype, {
return this._fingerprint;
},

/**
* @param {wikibase.datamodel.Fingerprint} fingerprint
*/
setFingerprint: function( fingerprint ) {
this._fingerprint = fingerprint;
},

/**
* Returns what type of Entity this is.
*
Expand Down
108 changes: 0 additions & 108 deletions src/Fingerprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,6 @@ $.extend( SELF.prototype, {
return this._labels.hasItemForKey( languageCode );
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term|null} term
*/
setLabel: function( languageCode, term ) {
if ( term === null || term.getText() === '' ) {
this._labels.removeItemByKey( languageCode );
} else {
this._labels.setItem( languageCode, term );
}
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term} label
*/
removeLabel: function( languageCode, label ) {
this._labels.removeItem( languageCode, label );
},

/**
* @param {string} languageCode
*/
removeLabelFor: function( languageCode ) {
this._labels.removeItemByKey( languageCode );
},

/**
* @return {wikibase.datamodel.TermMap}
*/
Expand Down Expand Up @@ -149,33 +122,6 @@ $.extend( SELF.prototype, {
return this._descriptions.hasItemForKey( languageCode );
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term|null} term
*/
setDescription: function( languageCode, term ) {
if ( term === null || term.getText() === '' ) {
this._descriptions.removeItemByKey( languageCode );
} else {
this._descriptions.setItem( languageCode, term );
}
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.Term} description
*/
removeDescription: function( languageCode, description ) {
this._descriptions.removeItem( languageCode, description );
},

/**
* @param {string} languageCode
*/
removeDescriptionFor: function( languageCode ) {
this._descriptions.removeItemByKey( languageCode );
},

/**
* @return {wikibase.datamodel.MultiTermMap}
*/
Expand Down Expand Up @@ -208,60 +154,6 @@ $.extend( SELF.prototype, {
return this._aliases.hasItemForKey( languageCode );
},

/**
* @param {string|wikibase.datamodel.MultiTermMap} languageCodeOrAliases
* @param {wikibase.datamodel.MultiTerm|null} [aliases]
*
* @throws {Error} when passing a MultiTerm without a language code.
* @throws {Error} when passing a MultiTermMap with a language code.
* @throws {Error} when neither passing a MultiTerm nor a MultiTermMap object.
*/
setAliases: function( languageCodeOrAliases, aliases ) {
var languageCode;

if( typeof languageCodeOrAliases === 'string' ) {
languageCode = languageCodeOrAliases;
} else {
aliases = languageCodeOrAliases;
}

if( aliases === null || aliases instanceof wb.datamodel.MultiTerm ) {
if( !languageCode ) {
throw new Error( 'Language code the wb.datamodel.MultiTerm object should be set '
+ 'for needs to be specified' );
}
if ( aliases === null || aliases.isEmpty() ) {
this._aliases.removeItemByKey( languageCode );
} else {
this._aliases.setItem( languageCode, aliases );
}
} else if( aliases instanceof wb.datamodel.MultiTermMap ) {
if( languageCode ) {
throw new Error( 'Unable to handle language code when setting a '
+ 'wb.datamodel.MultiTermMap' );
}
this._aliases = aliases;
} else {
throw new Error( 'Aliases need to be specified as wb.datamodel.MultiTerm or '
+ 'wb.datamodel.MultiTermMap instance' );
}
},

/**
* @param {string} languageCode
* @param {wikibase.datamodel.MultiTerm} aliases
*/
removeAliases: function( languageCode, aliases ) {
this._aliases.removeItem( languageCode, aliases );
},

/**
* @param {string} languageCode
*/
removeAliasesFor: function( languageCode ) {
this._aliases.removeItemByKey( languageCode );
},

/**
* @return {boolean}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Group.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ $.extend( SELF.prototype, {

/**
* @param {wikibase.datamodel.GroupableCollection} groupableCollection
*
* @throws {Error} when passed GroupableCollection instance contains an item whose key does not
* match the key registered with the Group instance.
* @private
*/
setItemContainer: function( groupableCollection ) {
if( !( groupableCollection instanceof wb.datamodel.GroupableCollection ) ) {
Expand Down
14 changes: 0 additions & 14 deletions src/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,6 @@ var SELF = wb.datamodel.Item = util.inherit(
return this._siteLinkSet;
},

/**
* @param {wikibase.datamodel.SiteLink} siteLink
*/
addSiteLink: function( siteLink ) {
this._siteLinkSet.setSiteLink( siteLink );
},

/**
* @param {wikibase.datamodel.SiteLink} siteLink
*/
removeSiteLink: function( siteLink ) {
this._siteLinkSet.removeSiteLink( siteLink );
},

/**
* @return {wikibase.datamodel.StatementGroupSet}
*/
Expand Down
38 changes: 12 additions & 26 deletions src/MultiTerm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@
*
* @param {string} languageCode
* @param {string[]} texts
*
* @throws {Error} when the language code is not a string or empty.
* @throws {Error} when texts is not an array.
*/
var SELF = wb.datamodel.MultiTerm = function WbDataModelMultiTerm( languageCode, texts ) {
this.setLanguageCode( languageCode );
this.setTexts( texts );
if( typeof languageCode !== 'string' || languageCode === '' ) {
throw new Error( 'Language code has to be a non-empty string' );
}
if( !$.isArray( texts ) ) {
throw new Error( 'texts needs to be an array of strings' );
}

this._languageCode = languageCode;
this._texts = texts;
};

/**
Expand All @@ -41,37 +51,13 @@ $.extend( SELF.prototype, {
return this._languageCode;
},

/**
* @param {string} languageCode
*
* @throws {Error} when the language code is not a string or empty.
*/
setLanguageCode: function( languageCode ) {
if( typeof languageCode !== 'string' || languageCode === '' ) {
throw new Error( 'Language code has to be a non-empty string' );
}
this._languageCode = languageCode;
},

/**
* @return {string[]}
*/
getTexts: function() {
return this._texts;
},

/**
* @param {string[]} texts
*
* @throws {Error} when texts is not an array.
*/
setTexts: function( texts ) {
if( !$.isArray( texts ) ) {
throw new Error( 'texts needs to be an array of strings' );
}
this._texts = texts;
},

/**
* @param {*} multiTerm
* @return {boolean}
Expand Down
7 changes: 0 additions & 7 deletions src/SiteLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ $.extend( SELF.prototype, {
return this._pageName;
},

/**
* @param {string[]} [badges=[]]
*/
setBadges: function( badges ) {
this._badges = badges || [];
},

/**
* @return {string[]}
*/
Expand Down
Loading