MCPcopy Index your code
hub / github.com/Effect-TS/effect / fromMap

Function fromMap

packages/effect/src/internal/configProvider.ts:153–199  ·  view source on GitHub ↗
(
  map: Map<string, string>,
  config?: Partial<ConfigProvider.ConfigProvider.FromMapConfig>
)

Source from the content-addressed store, hash-verified

151
152/** @internal */
153export const fromMap = (
154 map: Map<string, string>,
155 config?: Partial<ConfigProvider.ConfigProvider.FromMapConfig>
156): ConfigProvider.ConfigProvider => {
157 const { pathDelim, seqDelim } = Object.assign({ seqDelim: ",", pathDelim: "." }, config)
158 const makePathString = (path: ReadonlyArray<string>): string => pipe(path, Arr.join(pathDelim))
159 const unmakePathString = (pathString: string): ReadonlyArray<string> => pathString.split(pathDelim)
160 const mapWithIndexSplit = splitIndexInKeys(
161 map,
162 (str) => unmakePathString(str),
163 makePathString
164 )
165 const load = <A>(
166 path: ReadonlyArray<string>,
167 primitive: Config.Config.Primitive<A>,
168 split = true
169 ): Effect.Effect<Array<A>, ConfigError.ConfigError> => {
170 const pathString = makePathString(path)
171 const valueOpt = mapWithIndexSplit.has(pathString) ?
172 Option.some(mapWithIndexSplit.get(pathString)!) :
173 Option.none()
174 return pipe(
175 valueOpt,
176 core.mapError(() => configError.MissingData(path, `Expected ${pathString} to exist in the provided map`)),
177 core.flatMap((value) => parsePrimitive(value, path, primitive, seqDelim, split))
178 )
179 }
180 const enumerateChildren = (
181 path: ReadonlyArray<string>
182 ): Effect.Effect<HashSet.HashSet<string>, ConfigError.ConfigError> =>
183 core.sync(() => {
184 const keyPaths = Arr.fromIterable(mapWithIndexSplit.keys()).map(unmakePathString)
185 const filteredKeyPaths = keyPaths.filter((keyPath) => {
186 for (let i = 0; i < path.length; i++) {
187 const pathComponent = pipe(path, Arr.unsafeGet(i))
188 const currentElement = keyPath[i]
189 if (currentElement === undefined || pathComponent !== currentElement) {
190 return false
191 }
192 }
193 return true
194 }).flatMap((keyPath) => keyPath.slice(path.length, path.length + 1))
195 return HashSet.fromIterable(filteredKeyPaths)
196 })
197
198 return fromFlat(makeFlat({ load, enumerateChildren, patch: pathPatch.empty }))
199}
200
201const extend = <A, B>(
202 leftDef: (n: number) => A,

Callers 1

fromJsonFunction · 0.85

Calls 4

splitIndexInKeysFunction · 0.85
unmakePathStringFunction · 0.85
fromFlatFunction · 0.85
makeFlatFunction · 0.85

Tested by

no test coverage detected