github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/services/documents/fn.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  func NewFn(name string) Fn {
    26  	return Fn{
    27  		Name:          name,
    28  		Title:         "",
    29  		Description:   "",
    30  		Deprecated:    false,
    31  		Readonly:      false,
    32  		Authorization: false,
    33  		Param:         Unknown(),
    34  		Result:        Unknown(),
    35  		Errors:        make(Errors, 0, 1),
    36  	}
    37  }
    38  
    39  type Fn struct {
    40  	Name          string  `json:"name,omitempty" avro:"name"`
    41  	Title         string  `json:"title,omitempty" avro:"title"`
    42  	Description   string  `json:"description,omitempty" avro:"description"`
    43  	Deprecated    bool    `json:"deprecated,omitempty" avro:"deprecated"`
    44  	Internal      bool    `json:"internal,omitempty" avro:"internal"`
    45  	Readonly      bool    `json:"readonly,omitempty" avro:"readonly"`
    46  	Authorization bool    `json:"authorization,omitempty" avro:"authorization"`
    47  	Permission    bool    `json:"permission,omitempty" avro:"permission"`
    48  	Param         Element `json:"argument,omitempty" avro:"param"`
    49  	Result        Element `json:"result,omitempty" avro:"result"`
    50  	Errors        Errors  `json:"errors,omitempty" avro:"errors"`
    51  }
    52  
    53  func (fn Fn) SetInfo(title string, description string) Fn {
    54  	fn.Title = title
    55  	fn.Description = description
    56  	return fn
    57  }
    58  
    59  func (fn Fn) SetDeprecated(deprecated bool) Fn {
    60  	fn.Deprecated = deprecated
    61  	return fn
    62  }
    63  
    64  func (fn Fn) SetInternal(internal bool) Fn {
    65  	fn.Internal = internal
    66  	return fn
    67  }
    68  
    69  func (fn Fn) SetReadonly(readonly bool) Fn {
    70  	fn.Readonly = readonly
    71  	return fn
    72  }
    73  
    74  func (fn Fn) SetAuthorization(authorization bool) Fn {
    75  	fn.Authorization = authorization
    76  	return fn
    77  }
    78  
    79  func (fn Fn) SetPermission(permission bool) Fn {
    80  	fn.Permission = permission
    81  	return fn
    82  }
    83  
    84  func (fn Fn) SetParam(param Element) Fn {
    85  	fn.Param = param
    86  	return fn
    87  }
    88  
    89  func (fn Fn) SetResult(result Element) Fn {
    90  	fn.Result = result
    91  	return fn
    92  }
    93  
    94  func (fn Fn) AddError(err Error) Fn {
    95  	fn.Errors = fn.Errors.Add(err)
    96  	return fn
    97  }
    98  
    99  func (fn Fn) SetErrors(err string) Fn {
   100  	fn.Errors = NewErrors(err)
   101  	return fn
   102  }
   103  
   104  type Fns []Fn
   105  
   106  func (pp Fns) Len() int {
   107  	return len(pp)
   108  }
   109  
   110  func (pp Fns) Less(i, j int) bool {
   111  	return strings.Compare(pp[i].Name, pp[j].Name) < 0
   112  }
   113  
   114  func (pp Fns) Swap(i, j int) {
   115  	pp[i], pp[j] = pp[j], pp[i]
   116  }
   117  
   118  func (pp Fns) Add(fn Fn) Fns {
   119  	n := append(pp, fn)
   120  	sort.Sort(n)
   121  	return n
   122  }