Skip to content

Commit

Permalink
Merge pull request #37 from FeatureServer/bug/id-field-type
Browse files Browse the repository at this point in the history
set correct id field type
  • Loading branch information
Daniel Fenton authored Jul 8, 2017
2 parents 3013ec9 + 86ffa63 commit 69d17f0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased
### Fixed
* ObjectID Fields set to esriFieldTypeOID

## [2.3.10] - 2017-07-07
### Rebase
### Fixed
* Added package details from [2.3.9]

## [2.3.9] - 2017-07-07
Expand Down
57 changes: 31 additions & 26 deletions src/field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fieldMap = require('./field-map')
const createFieldAliases = require('./aliases')
const createStatFields = require('./statFields')

module.exports = { computeFields, computeFieldObject, createStatFields, createFieldAliases }
module.exports = { computeFieldsFromProperties, computeFieldObject, createStatFields, createFieldAliases }

const templates = {
server: require('../../templates/server.json'),
Expand All @@ -15,6 +15,32 @@ const templates = {
objectIDField: require('../../templates/oid-field.json')
}

// TODO this should be the only exported function
function computeFieldObject (data, template, options) {
let oid = false
const metadata = data.metadata || {}

if (!metadata.fields && data.statistics) return computeFieldsFromProperties(data.statistics[0], template, options).fields
else if (!metadata.fields) return computeAggFieldObject(data, template, options)

const fields = metadata.fields.map(field => {
let type
if (field.name === metadata.idField || (!metadata.idField && field.name.toLowerCase() === 'objectid')) {
type = 'esriFieldTypeOID'
oid = true
}
const template = _.cloneDeep(templates.field)
return Object.assign({}, template, {
name: field.name,
type: type || fieldMap[field.type.toLowerCase()] || field.type,
alias: field.alias || field.name
})
})

if (!oid) fields.push(templates.objectIDField)
return fields
}

/** @type {Array} accepted date formats used by moment.js */
const DATE_FORMATS = [moment.ISO_8601]

Expand All @@ -26,7 +52,7 @@ const DATE_FORMATS = [moment.ISO_8601]
* @param {object} options
* @return {object} fields
*/
function computeFields (props, template, options) {
function computeFieldsFromProperties (props, template, options = {}) {
const fields = Object.keys(props).map((key, i) => {
const type = fieldType(props[key])
const field = { name: key, type: type, alias: key }
Expand All @@ -38,7 +64,7 @@ function computeFields (props, template, options) {
if (template === 'layer' && Object.keys(props).indexOf('OBJECTID') < 0) {
fields.push({
name: 'OBJECTID',
type: 'esriFieldTypeInteger',
type: 'esriFieldTypeOID',
alias: 'OBJECTID'
})
}
Expand Down Expand Up @@ -74,30 +100,9 @@ function isInt (value) {
return Math.round(value) === value
}

function computeFieldObject (data, template, options) {
let oid = false
const metadata = data.metadata || {}

if (!metadata.fields && data.statistics) return computeFields(data.statistics[0], template, options).fields
else if (!metadata.fields) return computeAggFieldObject(data, template, options)

const fields = metadata.fields.map(field => {
if (field.name === metadata.idField || field.name.toLowerCase() === 'objectid') oid = true
const template = _.cloneDeep(templates.field)
return Object.assign({}, template, {
name: field.name,
type: fieldMap[field.type.toLowerCase()] || field.type,
alias: field.alias || field.name
})
})

if (!oid) fields.push(templates.objectIDField)
return fields
}

function computeAggFieldObject (data, template, options = {}) {
const feature = (data.features && data.features[0])
const feature = data.features && data.features[0]
const properties = feature ? feature.properties || feature.attributes : options.attributeSample
if (properties) return computeFields(properties, template, options).fields
if (properties) return computeFieldsFromProperties(properties, template, options).fields
else return []
}
4 changes: 2 additions & 2 deletions test/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('when building esri fields', function () {
propString: 'Awesome',
propDate: '2015-06-22T13:17:21+0000'
}
var fieldObj = field.computeFields(input)
var fieldObj = field.computeFieldsFromProperties(input)
var fields = fieldObj.fields

it('fields should be an array', () => {
Expand Down Expand Up @@ -38,7 +38,7 @@ describe('when building esri fields', function () {
improperDate1: 'Thisisafaildate 1',
improperDate2: '06/22/2015'
}
const fieldObj = field.computeFields(input)
const fieldObj = field.computeFieldsFromProperties(input)
const fields = fieldObj.fields

it('Should not allow improper date formats through', () => {
Expand Down
18 changes: 18 additions & 0 deletions test/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ describe('Info operations', () => {
layer.displayField.should.equal('test')
layer.timeInfo.test.should.equal('test')
})

it('should assign esriFieldTypeOID to the idField', () => {
const input = {
metadata: {
idField: 'test',
geometryType: 'Polygon',
extent: [[11, 12], [13, 14]],
fields: [
{
name: 'test',
type: 'integer'
}
]
}
}
const layer = FeatureServer.layerInfo(input, {})
layer.fields[0].type.should.equal('esriFieldTypeOID')
})
})

describe('when getting featureserver info from geojson', () => {
Expand Down

0 comments on commit 69d17f0

Please sign in to comment.