All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.partiql.lang.eval.BindingsExtensions.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0-perf.1
Show newest version
/*
 * Copyright 2019 Amazon.com, Inc. or its affiliates.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 *  You may not use this file except in compliance with the License.
 * A copy of the License is located at:
 *
 *      http://aws.amazon.com/apache2.0/
 *
 *  or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 *  language governing permissions and limitations under the License.
 */

package org.partiql.lang.eval

/**
 * Wraps these [Bindings] to delegate lookup to another instance when lookup on this
 * one fails.
 *
 * Note that this doesn't modify an existing [Bindings] but creates a new instance that
 * does delegation.
 *
 * @param fallback The bindings to delegate to when lookup fails to find a name.
 */
fun  Bindings.delegate(fallback: Bindings): Bindings =
    object : Bindings {
        override fun get(bindingName: BindingName): T? {
            val binding = this@delegate[bindingName]
            return binding ?: fallback[bindingName]
        }
    }

/**
 * Wraps a binding with a set of names that should not be resolved to anything.
 *
 * @receiver The [Bindings] to delegate over.
 * @param names, the deny listed names
 */
@Deprecated(
    message = "To be replaced with functionally equivalent denyList method.",
    replaceWith = ReplaceWith("denyList", "org.partiql.lang.eval.denyList"),
    level = DeprecationLevel.WARNING
)
fun  Bindings.blacklist(vararg names: String) = this.denyList(*names)

/**
 * Wraps a binding with a set of names that should not be resolved to anything.
 *
 * @receiver The [Bindings] to delegate over.
 * @param names, the deny listed names
 */
fun  Bindings.denyList(vararg names: String) = object : Bindings {
    val denyListed = names.toSet()
    val loweredDenyListed = names.map { it.toLowerCase() }.toSet()

    override fun get(bindingName: BindingName): T? {
        val isDenyListed = when (bindingName.bindingCase) {
            BindingCase.SENSITIVE -> denyListed.contains(bindingName.name)
            BindingCase.INSENSITIVE -> loweredDenyListed.contains(bindingName.loweredName)
        }
        return when {
            isDenyListed -> null
            else -> this[bindingName]
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy