Skip to content

Commit

Permalink
Refactor amp-geo to use externs, (ampproject#20015)
Browse files Browse the repository at this point in the history
* Refactor amp-geo to use externs,
update dependant extension to use isInCountryGroup()

* isInGeoGroup() returns enum.

* bad path

* revert externs

* remove stray newline

* change config to prevent single pass closure mangle

* add new owners
  • Loading branch information
jpettitt authored Jan 10, 2019
1 parent b5b40b3 commit e8e52a9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 22 deletions.
3 changes: 2 additions & 1 deletion extensions/amp-consent/0.1/amp-consent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {ConsentPolicyManager} from './consent-policy-manager';
import {ConsentStateManager} from './consent-state-manager';
import {ConsentUI} from './consent-ui';
import {Deferred} from '../../../src/utils/promise';
import {GEO_IN_GROUP} from '../../amp-geo/0.1/amp-geo';
import {
NOTIFICATION_UI_MANAGER,
NotificationUiManager,
Expand Down Expand Up @@ -451,7 +452,7 @@ export class AmpConsent extends AMP.BaseElement {
return Services.geoForDocOrNull(this.element).then(geo => {
userAssert(geo,
'requires <amp-geo> to use promptIfUnknownForGeoGroup');
return (geo.ISOCountryGroups.indexOf(geoGroup) >= 0);
return (geo.isInCountryGroup(geoGroup) == GEO_IN_GROUP.IN);
});
}

Expand Down
5 changes: 4 additions & 1 deletion extensions/amp-consent/0.1/test/test-amp-consent.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
AmpConsent,
} from '../amp-consent';
import {CONSENT_ITEM_STATE} from '../consent-info';
import {GEO_IN_GROUP} from '../../../amp-geo/0.1/amp-geo';
import {dict} from '../../../../src/utils/object';
import {macroTask} from '../../../../testing/yield';
import {
Expand Down Expand Up @@ -75,7 +76,9 @@ describes.realWin('amp-consent', {
resetServiceForTesting(win, 'geo');
registerServiceBuilder(win, 'geo', function() {
return Promise.resolve({
'ISOCountryGroups': ISOCountryGroups,
isInCountryGroup: group =>
ISOCountryGroups.indexOf(group) >= 0 ?
GEO_IN_GROUP.IN : GEO_IN_GROUP.NOT_IN,
});
});

Expand Down
27 changes: 10 additions & 17 deletions extensions/amp-geo/0.1/amp-geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ const mode = {
GEO_OVERRIDE: 2, // We've been overriden in test by #amp-geo=xx
};


/**
* @typedef {{
* ISOCountry: string,
* matchedISOCountryGroups: !Array<string>,
* allISOCountryGroups: !Array<string>,
* isInCountryGroup: GEO_IN_GROUP,
* ISOCountryGroups: !Array<string>
* }}
*/
* @typedef {{
* ISOCountry: string,
* matchedISOCountryGroups: !Array<string>,
* allISOCountryGroups: !Array<string>,
* isInCountryGroup: (function(string):GEO_IN_GROUP),
* }}
*/
export let GeoDef;


Expand Down Expand Up @@ -213,7 +213,7 @@ export class AmpGeo extends AMP.BaseElement {
matchCountryGroups_(config) {
// ISOCountryGroups are optional but if specified at least one must exist
const ISOCountryGroups = /** @type {!Object<string, !Array<string>>} */(
config.ISOCountryGroups);
config['ISOCountryGroups']);
const errorPrefix = '<amp-geo> ISOCountryGroups'; // code size
if (ISOCountryGroups) {
this.assertWithErrorReturn_(
Expand Down Expand Up @@ -336,7 +336,7 @@ export class AmpGeo extends AMP.BaseElement {

// Only include amp state if user requests it to
// avoid validator issue with missing amp-bind js
if (config.AmpBind) {
if (config['AmpBind']) {
const geoState = doc.getElementById(GEO_ID);
if (geoState) {
geoState.parentNode.removeChild(geoState);
Expand All @@ -363,13 +363,6 @@ export class AmpGeo extends AMP.BaseElement {
allISOCountryGroups: this.definedGroups_,
/* API */
isInCountryGroup: this.isInCountryGroup.bind(this),
/**
* Temp still return old interface to avoid version skew
* with consuming extensions. This will go away don't use it!
* replace with matchedISOCountryGroups or use the isInCountryGroup
* API
*/
ISOCountryGroups: self.matchedGroups_,
};
});
}
Expand Down
2 changes: 2 additions & 0 deletions extensions/amp-geo/OWNERS.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
- jpettitt
- zhoux
- lannka
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {CSS} from '../../../build/amp-user-notification-0.1.css';
import {Deferred} from '../../../src/utils/promise';
import {GEO_IN_GROUP} from '../../amp-geo/0.1/amp-geo';
import {
NOTIFICATION_UI_MANAGER,
NotificationUiManager,
Expand Down Expand Up @@ -217,7 +218,7 @@ export class AmpUserNotification extends AMP.BaseElement {
'requires <amp-geo> to use promptIfUnknownForGeoGroup');

const matchedGeos = geoGroup.split(/,\s*/).filter(group => {
return geo.ISOCountryGroups.indexOf(group) >= 0;
return geo.isInCountryGroup(group) == GEO_IN_GROUP.IN;
});

// Invert if includeGeos is false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
AmpUserNotification,
UserNotificationManager,
} from '../amp-user-notification';
import {GEO_IN_GROUP} from '../../../amp-geo/0.1/amp-geo';
import {
getServiceForDoc,
getServicePromiseForDoc,
Expand Down Expand Up @@ -54,7 +55,9 @@ describes.realWin('amp-user-notification', {
resetServiceForTesting(win, 'geo');
registerServiceBuilder(win, 'geo', function() {
return Promise.resolve({
'ISOCountryGroups': ISOCountryGroups,
isInCountryGroup: group =>
ISOCountryGroups.indexOf(group) >= 0 ?
GEO_IN_GROUP.IN : GEO_IN_GROUP.NOT_IN,
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/service/url-replacements-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export class GlobalVariableSource extends VariableSource {
'The value passed to AMP_GEO() is not valid name:' + geoType);
return /** @type {string} */ (geos[geoType] || 'unknown');
}
return /** @type {string} */ (geos.ISOCountryGroups.join(GEO_DELIM));
return /** @type {string} */ (geos.matchedISOCountryGroups.join(GEO_DELIM));
}, 'AMP_GEO');
}));

Expand Down

0 comments on commit e8e52a9

Please sign in to comment.