github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/services/documents/properties.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     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   */
    17  
    18  package documents
    19  
    20  import (
    21  	"sort"
    22  	"strings"
    23  )
    24  
    25  type Property struct {
    26  	Name    string  `json:"name" avro:"name"`
    27  	Element Element `json:"element" avro:"element"`
    28  }
    29  
    30  type Properties []Property
    31  
    32  func (pp Properties) Len() int {
    33  	return len(pp)
    34  }
    35  
    36  func (pp Properties) Less(i, j int) bool {
    37  	return strings.Compare(pp[i].Name, pp[j].Name) < 0
    38  }
    39  
    40  func (pp Properties) Swap(i, j int) {
    41  	pp[i], pp[j] = pp[j], pp[i]
    42  }
    43  
    44  func (pp Properties) Add(name string, element Element) Properties {
    45  	n := append(pp, Property{
    46  		Name:    name,
    47  		Element: element,
    48  	})
    49  	sort.Sort(n)
    50  	return n
    51  }
    52  
    53  func (pp Properties) Get(name string) (p Property, has bool) {
    54  	for _, property := range pp {
    55  		if property.Name == name {
    56  			p = property
    57  			has = true
    58  			return
    59  		}
    60  	}
    61  	return
    62  }