...

Source file src/github.com/ory/fosite/arguments.go

Documentation: github.com/ory/fosite

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   *
    20   */
    21  
    22  package fosite
    23  
    24  import "strings"
    25  
    26  type Arguments []string
    27  
    28  // Matches performs an case-insensitive, out-of-order check that the items
    29  // provided exist and equal all of the args in arguments.
    30  // Note:
    31  // - Providing a list that includes duplicate string-case items will return not
    32  //   matched.
    33  func (r Arguments) Matches(items ...string) bool {
    34  	if len(r) != len(items) {
    35  		return false
    36  	}
    37  
    38  	found := make(map[string]bool)
    39  	for _, item := range items {
    40  		if !StringInSlice(item, r) {
    41  			return false
    42  		}
    43  		found[item] = true
    44  	}
    45  
    46  	return len(found) == len(r)
    47  }
    48  
    49  // Has checks, in a case-insensitive manner, that all of the items
    50  // provided exists in arguments.
    51  func (r Arguments) Has(items ...string) bool {
    52  	for _, item := range items {
    53  		if !StringInSlice(item, r) {
    54  			return false
    55  		}
    56  	}
    57  
    58  	return true
    59  }
    60  
    61  // HasOneOf checks, in a case-insensitive manner, that one of the items
    62  // provided exists in arguments.
    63  func (r Arguments) HasOneOf(items ...string) bool {
    64  	for _, item := range items {
    65  		if StringInSlice(item, r) {
    66  			return true
    67  		}
    68  	}
    69  
    70  	return false
    71  }
    72  
    73  // Deprecated: Use ExactOne, Matches or MatchesExact
    74  func (r Arguments) Exact(name string) bool {
    75  	return name == strings.Join(r, " ")
    76  }
    77  
    78  // ExactOne checks, by string case, that a single argument equals the provided
    79  // string.
    80  func (r Arguments) ExactOne(name string) bool {
    81  	return len(r) == 1 && r[0] == name
    82  }
    83  
    84  // MatchesExact checks, by order and string case, that the items provided equal
    85  // those in arguments.
    86  func (r Arguments) MatchesExact(items ...string) bool {
    87  	if len(r) != len(items) {
    88  		return false
    89  	}
    90  
    91  	for i, item := range items {
    92  		if item != r[i] {
    93  			return false
    94  		}
    95  	}
    96  
    97  	return true
    98  }
    99  

View as plain text