Skip to content

Commit

Permalink
Keeping functions immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Apr 30, 2020
1 parent 5301a3f commit 22b4a69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ test('tests processing text field with multi fields', () => {
const fields: Field[] = safeLoad(textWithMultiFieldsLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(textWithMultiFieldsMapping));
expect(mappings).toEqual(textWithMultiFieldsMapping);
});

test('tests processing keyword field with multi fields', () => {
Expand Down Expand Up @@ -127,7 +127,7 @@ test('tests processing keyword field with multi fields', () => {
const fields: Field[] = safeLoad(keywordWithMultiFieldsLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(keywordWithMultiFieldsMapping));
expect(mappings).toEqual(keywordWithMultiFieldsMapping);
});

test('tests processing keyword field with multi fields with analyzed text field', () => {
Expand Down Expand Up @@ -159,7 +159,7 @@ test('tests processing keyword field with multi fields with analyzed text field'
const fields: Field[] = safeLoad(keywordWithAnalyzedMultiFieldsLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(keywordWithAnalyzedMultiFieldsMapping));
expect(mappings).toEqual(keywordWithAnalyzedMultiFieldsMapping);
});

test('tests processing object field with no other attributes', () => {
Expand All @@ -177,7 +177,7 @@ test('tests processing object field with no other attributes', () => {
const fields: Field[] = safeLoad(objectFieldLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(objectFieldMapping));
expect(mappings).toEqual(objectFieldMapping);
});

test('tests processing object field with enabled set to false', () => {
Expand All @@ -197,7 +197,7 @@ test('tests processing object field with enabled set to false', () => {
const fields: Field[] = safeLoad(objectFieldEnabledFalseLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(objectFieldEnabledFalseMapping));
expect(mappings).toEqual(objectFieldEnabledFalseMapping);
});

test('tests processing object field with dynamic set to false', () => {
Expand All @@ -217,7 +217,7 @@ test('tests processing object field with dynamic set to false', () => {
const fields: Field[] = safeLoad(objectFieldDynamicFalseLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(objectFieldDynamicFalseMapping));
expect(mappings).toEqual(objectFieldDynamicFalseMapping);
});

test('tests processing object field with dynamic set to true', () => {
Expand All @@ -237,7 +237,7 @@ test('tests processing object field with dynamic set to true', () => {
const fields: Field[] = safeLoad(objectFieldDynamicTrueLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(objectFieldDynamicTrueMapping));
expect(mappings).toEqual(objectFieldDynamicTrueMapping);
});

test('tests processing object field with dynamic set to strict', () => {
Expand All @@ -257,7 +257,7 @@ test('tests processing object field with dynamic set to strict', () => {
const fields: Field[] = safeLoad(objectFieldDynamicStrictLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(objectFieldDynamicStrictMapping));
expect(mappings).toEqual(objectFieldDynamicStrictMapping);
});

test('tests processing object field with property', () => {
Expand All @@ -282,7 +282,7 @@ test('tests processing object field with property', () => {
const fields: Field[] = safeLoad(objectFieldWithPropertyLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(objectFieldWithPropertyMapping));
expect(mappings).toEqual(objectFieldWithPropertyMapping);
});

test('tests processing object field with property, reverse order', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ export function generateMappings(fields: Field[]): IndexTemplateMappings {

switch (type) {
case 'group':
fieldProps = generateMappings(field.fields!);
attemptAddDynamicAndEnabled(fieldProps, field);
fieldProps = { ...generateMappings(field.fields!), ...generateDynamicAndEnabled(field) };
break;
case 'group-nested':
fieldProps = generateMappings(field.fields!);
fieldProps.type = 'nested';
attemptAddNestedProps(fieldProps, field);
fieldProps = {
...generateMappings(field.fields!),
...generateNestedProps(field),
type: 'nested',
};
break;
case 'integer':
fieldProps.type = 'long';
Expand All @@ -101,12 +102,10 @@ export function generateMappings(fields: Field[]): IndexTemplateMappings {
}
break;
case 'object':
fieldProps.type = 'object';
attemptAddDynamicAndEnabled(fieldProps, field);
fieldProps = { ...fieldProps, ...generateDynamicAndEnabled(field), type: 'object' };
break;
case 'nested':
fieldProps.type = 'nested';
attemptAddNestedProps(fieldProps, field);
fieldProps = { ...fieldProps, ...generateNestedProps(field), type: 'nested' };
break;
case 'array':
// this assumes array fields were validated in an earlier step
Expand All @@ -133,24 +132,27 @@ export function generateMappings(fields: Field[]): IndexTemplateMappings {
return { properties: props };
}

function attemptAddDynamicAndEnabled(props: Properties, field: Field) {
function generateDynamicAndEnabled(field: Field) {
const props: Properties = {};
if (field.hasOwnProperty('enabled')) {
props.enabled = field.enabled;
}
if (field.hasOwnProperty('dynamic')) {
props.dynamic = field.dynamic;
}
return props;
}

function attemptAddNestedProps(props: Properties, field: Field) {
attemptAddDynamicAndEnabled(props, field);
function generateNestedProps(field: Field) {
const props = generateDynamicAndEnabled(field);

if (field.hasOwnProperty('include_in_parent')) {
props.include_in_parent = field.include_in_parent;
}
if (field.hasOwnProperty('include_in_root')) {
props.include_in_root = field.include_in_root;
}
return props;
}

function generateMultiFields(fields: Fields): MultiFields {
Expand Down

0 comments on commit 22b4a69

Please sign in to comment.