github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/services/documents/endpoint.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  	"github.com/aacfactory/fns/commons/versions"
    22  )
    23  
    24  func New(name string, title string, description string, version versions.Version) Endpoint {
    25  	return Endpoint{
    26  		Version:     version,
    27  		Name:        name,
    28  		Title:       title,
    29  		Description: description,
    30  		Functions:   make(Fns, 0, 1),
    31  		Elements:    make(Elements, 0, 1),
    32  	}
    33  }
    34  
    35  type Endpoint struct {
    36  	Version     versions.Version `json:"version" avro:"version"`
    37  	Name        string           `json:"name" avro:"name"`
    38  	Title       string           `json:"title" avro:"title"`
    39  	Description string           `json:"description" avro:"description"`
    40  	Internal    bool             `json:"internal" avro:"internal"`
    41  	Functions   Fns              `json:"functions" avro:"functions"`
    42  	Elements    Elements         `json:"elements" avro:"elements"`
    43  }
    44  
    45  func (endpoint *Endpoint) Defined() bool {
    46  	return endpoint.Name != ""
    47  }
    48  
    49  func (endpoint *Endpoint) SetInternal() {
    50  	endpoint.Internal = true
    51  }
    52  
    53  func (endpoint *Endpoint) AddFn(fn Fn) {
    54  	if fn.Param.Exist() {
    55  		if !fn.Readonly {
    56  			paramRef := endpoint.addElement(fn.Param)
    57  			fn.Param = paramRef
    58  		}
    59  	}
    60  	if fn.Result.Exist() {
    61  		paramRef := endpoint.addElement(fn.Result)
    62  		fn.Result = paramRef
    63  	}
    64  	endpoint.Functions = endpoint.Functions.Add(fn)
    65  }
    66  
    67  func (endpoint *Endpoint) addElement(element Element) (ref Element) {
    68  	if !element.Exist() {
    69  		ref = element
    70  		return
    71  	}
    72  	if element.IsRef() || element.IsAny() {
    73  		ref = element
    74  		return
    75  	}
    76  	unpacks := unpack(element)
    77  	ref = unpacks[0]
    78  	for _, unpacked := range unpacks {
    79  		if unpacked.IsBuiltin() || unpacked.IsRef() || unpacked.Path == "" {
    80  			continue
    81  		}
    82  		endpoint.Elements = endpoint.Elements.Add(unpacked)
    83  	}
    84  	return
    85  }