private applySectionsToParsedIni()

in server/aws-lsp-identity/src/language-server/profiles/sharedConfigProfileStore.ts [140:209]


    private applySectionsToParsedIni<T extends Section>(
        sectionType: IniSectionType.PROFILE | IniSectionType.SSO_SESSION, // SERVICES not currently supported
        sections: T[],
        parsedKnownFiles: ParsedIniData,
        validator: (section: T, parsedSection: IniSection) => boolean
    ): void {
        const throwAwsError = (message: string) => {
            throw new AwsError(
                message,
                sectionType === IniSectionType.PROFILE
                    ? AwsErrorCodes.E_INVALID_PROFILE
                    : AwsErrorCodes.E_INVALID_SSO_SESSION
            )
        }

        for (const section of sections) {
            if (!section?.name) {
                throwAwsError('Section name is required.')
            }

            const parsedSectionName = new SectionHeader(section.name, sectionType).toParsedSectionName()

            // Remove sections that have no settings
            if (
                section.settings === undefined ||
                section.settings === null ||
                Object.keys(section.settings).length === 0
            ) {
                delete parsedKnownFiles[parsedSectionName]
                continue
            }

            // Settings must be an object
            if (section.settings !== Object(section.settings)) {
                throwAwsError('Section contains invalid settings value.')
            }

            const parsedSection = (parsedKnownFiles[parsedSectionName] ||= {})

            // eslint-disable-next-line prefer-const
            for (let [name, value] of Object.entries(section.settings)) {
                if (Array.isArray(value)) {
                    value = normalizeSettingList(value)?.join(',') ?? ''
                }

                // If and when needed in the future, handle object types for subsections (e.g. api_versions)
                if (value === Object(value)) {
                    throwAwsError(`Setting [${name}] cannot be an object.`)
                }

                // If setting passed with null or undefined then remove setting
                // If setting passed with any other value then update setting
                // If setting not passed then preserve setting in file as-is
                value = value?.toString().trim()
                if (value === undefined || value === null || value === '') {
                    Object.hasOwn(parsedSection, name) && delete parsedSection[name]
                } else {
                    if (controlCharsRegex.test(value)) {
                        throwAwsError(`Setting [${name}] cannot contain control characters.`)
                    }

                    parsedSection[name] = value.toString()
                }
            }

            if (!validator(section, parsedSection)) {
                throwAwsError('Section is invalid.')
            }
        }
    }