import * as fs from 'fs' ; import * as _ from 'lodash' ; import * as vscode from 'vscode' ; import { yamlLocator , YamlMap } from './yaml-locator' ; import { util as yamlUtil } from 'node-yaml-parser' ; import { GROUP_VERSION_KIND_SEPARATOR , KUBERNETES_SCHEMA_PREFIX } from "./yaml-constant" ; import { Dictionary } from '../utils/dictionary' ; export enum StringComparison { Ordinal , OrdinalIgnoreCase } param { vscode . TextDocument } doc param { vscode . Position } pos returns { boolean } export function isPositionInKey ( doc , pos ) { if ( ! doc || ! pos ) { return false ; } const element = yamlLocator . getMatchedElement ( doc , pos ) ; return element ? yamlUtil . isKey ( element . matchedNode ) : false ; } param { string } file returns export function loadJson ( file ) { if ( fs . existsSync ( file ) ) { try { return JSON . parse ( fs . readFileSync ( file , 'utf-8' ) ) ; } catch ( err ) { } } return undefined ; } param { string [ ] } ids returns { string } export function makeKubernetesUri ( ids ) { if ( ! ids ) { throw new Error ( "'id' is required for constructing a schema uri." ) ; } if ( _ . isString ( ids ) ) { return KUBERNETES_SCHEMA_PREFIX + ( < string > ids ) . toLowerCase ( ) ; } if ( ids . length === 1 ) { return makeKubernetesUri ( ids [ 0 ] ) ; } else if ( ids . length > 1 ) { return KUBERNETES_SCHEMA_PREFIX + ids . map ( ( id ) => id . toLowerCase ( ) ) . join ( '+' ) ; } else { return undefined ; } } export function makeRefOnKubernetes ( id ) { const uri = makeKubernetesUri ( id ) ; return uri ? { $ref : uri } : undefined ; } export function parseKubernetesGroupVersionKind ( groupKindNodeItem ) { const group = getStringValue ( groupKindNodeItem , 'group' , StringComparison . OrdinalIgnoreCase ) ; const version = getStringValue ( groupKindNodeItem , 'version' , StringComparison . OrdinalIgnoreCase ) ; const apiVersion = group ? `${ group }/${ version }` : version ; const kind = getStringValue ( groupKindNodeItem , 'kind' , StringComparison . OrdinalIgnoreCase ) ; return apiVersion && kind ? { id : apiVersion + GROUP_VERSION_KIND_SEPARATOR + kind , apiVersion , kind } : undefined ; } export function equalIgnoreCase ( a , b ) { return _ . isString ( a ) && _ . isString ( b ) && a . toLowerCase ( ) === b . toLowerCase ( ) ; } export function getYamlMappingValue ( mapRootNode , key , ignoreCase = StringComparison . Ordinal ) { if ( ! key ) { return undefined ; } const keyValueItem = mapRootNode . mappings . find ( ( mapping ) => mapping . key && ( ignoreCase === StringComparison . OrdinalIgnoreCase ? key === mapping . key . raw : equalIgnoreCase ( key , mapping . key . raw ) ) ) ; return keyValueItem ? keyValueItem . value . raw : undefined ; } function getStringValue ( node , key , ignoreCase = StringComparison . Ordinal ) { if ( ! node ) { return undefined ; } if ( node . hasOwnProperty ( key ) ) { return node [ key ] ; } if ( ignoreCase === StringComparison . OrdinalIgnoreCase ) { for ( const nodeKey of Object . keys ( node ) ) { if ( equalIgnoreCase ( key , nodeKey ) ) { return node [ nodeKey ] ; } } } return undefined ; }