Skip to content

Commit

Permalink
with default config (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
noamhammer committed Mar 8, 2020
1 parent 26a9d96 commit 329d778
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
23 changes: 17 additions & 6 deletions packages/core/src/core/adapters/adapters.ts
Expand Up @@ -15,10 +15,11 @@
*/
import _ from 'lodash'
import {
ObjectType, Adapter, ElemIdGetter, AdapterCreatorOpts,
ObjectType, Adapter, ElemIdGetter, AdapterCreatorOpts, ElemID,
} from '@salto-io/adapter-api'
import adapterCreators from './creators'
import { ConfigSource } from '../../workspace/config_source'
import { createDefaultInstanceFromType } from '../merger/internal/instances'

export const getAdaptersCredentialsTypes = (
names: string[]
Expand Down Expand Up @@ -51,11 +52,21 @@ export const getAdapters = async (
): Promise<Record<string, Adapter>> => {
const creatorConfig: Record<string, AdapterCreatorOpts> = _
.fromPairs(await Promise.all(adapters.map(
async adapter => ([adapter, {
credentials: await credentials.get(adapter),
config: await config.get(adapter),
getElemIdFunc: elemIdGetter,
}])
async adapter => {
const adapterConfig = await config.get(adapter)
const { configType } = adapterCreators[adapter]
const defaultConfig = configType
? createDefaultInstanceFromType(ElemID.CONFIG_NAME, configType)
: undefined
if (_.isUndefined(adapterConfig) && defaultConfig) {
await config.set(adapter, defaultConfig)
}
return ([adapter, {
credentials: await credentials.get(adapter),
config: adapterConfig ?? defaultConfig,
getElemIdFunc: elemIdGetter,
}])
}
)))
return initAdapters(creatorConfig)
}
2 changes: 1 addition & 1 deletion packages/core/src/core/merger/index.ts
Expand Up @@ -32,7 +32,7 @@ export {
DuplicateAnnotationFieldDefinitionError, DuplicateAnnotationTypeError,
} from './internal/object_types'

export { DuplicateInstanceKeyError } from './internal/instances'
export { DuplicateInstanceKeyError, createDefaultInstanceFromType } from './internal/instances'
export { MultiplePrimitiveTypesUnsupportedError } from './internal/primitives'

const log = logger(module)
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/core/merger/internal/instances.ts
Expand Up @@ -92,3 +92,6 @@ export const mergeInstances = (
errors.length}]`)
return { merged, errors }
}

export const createDefaultInstanceFromType = (name: string, objectType: ObjectType):
InstanceElement => mergeInstances([new InstanceElement(name, objectType)]).merged[0]
23 changes: 21 additions & 2 deletions packages/core/test/merger.test.ts
Expand Up @@ -15,14 +15,14 @@
*/
import {
ObjectType, ElemID, Field, BuiltinTypes, InstanceElement, PrimitiveType,
PrimitiveTypes, TypeElement,
PrimitiveTypes, TypeElement, CORE_ANNOTATIONS,
} from '@salto-io/adapter-api'
import {
mergeElements,
MultipleBaseDefinitionsMergeError,
NoBaseDefinitionMergeError, DuplicateAnnotationTypeError,
DuplicateAnnotationError, DuplicateAnnotationFieldDefinitionError, DuplicateInstanceKeyError,
MultiplePrimitiveTypesUnsupportedError,
MultiplePrimitiveTypesUnsupportedError, createDefaultInstanceFromType,
} from '../src/core/merger'
import { Keywords } from '../src/parser/language'

Expand Down Expand Up @@ -365,6 +365,25 @@ describe('merger', () => {
expect(errors).toHaveLength(1)
expect(errors[0]).toBeInstanceOf(DuplicateAnnotationError)
})

it('should create default instance from type', () => {
const mockElemID = new ElemID('test')
const configType = new ObjectType({
elemID: mockElemID,
fields: {
val1: new Field(
mockElemID,
'val1',
BuiltinTypes.STRING,
{
[CORE_ANNOTATIONS.DEFAULT]: 'test',
}
),
},
})
expect(createDefaultInstanceFromType('test', configType))
.toEqual(new InstanceElement('test', configType, { val1: 'test' }))
})
})

describe('merging primitives', () => {
Expand Down
18 changes: 15 additions & 3 deletions packages/salesforce-adapter/src/adapter_creator.ts
Expand Up @@ -16,7 +16,7 @@
import { collections } from '@salto-io/lowerdash'
import { logger } from '@salto-io/logging'
import {
BuiltinTypes, ObjectType, ElemID, InstanceElement, Field, AdapterCreator,
BuiltinTypes, ObjectType, ElemID, InstanceElement, Field, AdapterCreator, CORE_ANNOTATIONS,
} from '@salto-io/adapter-api'
import SalesforceClient, { Credentials, validateCredentials } from './client/client'
import { changeValidator } from './change_validator'
Expand All @@ -43,10 +43,22 @@ const configType = new ObjectType({
elemID: configID,
fields: {
metadataTypesBlacklist: new Field(
configID, 'metadataTypesBlacklist', BuiltinTypes.STRING, {}, true,
configID,
'metadataTypesBlacklist',
BuiltinTypes.STRING,
{
[CORE_ANNOTATIONS.DEFAULT]: [],
},
true,
),
instancesRegexBlacklist: new Field(
configID, 'instancesRegexBlacklist', BuiltinTypes.STRING, {}, true,
configID,
'instancesRegexBlacklist',
BuiltinTypes.STRING,
{
[CORE_ANNOTATIONS.DEFAULT]: [],
},
true,
),
},
})
Expand Down

0 comments on commit 329d778

Please sign in to comment.