github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/cmd/generates/sources/imports.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 sources
    19  
    20  import (
    21  	"fmt"
    22  	"go/ast"
    23  	"strings"
    24  )
    25  
    26  func NewImportsFromAstFileImports(specs []*ast.ImportSpec) (v Imports) {
    27  	v = Imports{}
    28  	if specs == nil || len(specs) == 0 {
    29  		return
    30  	}
    31  	for _, spec := range specs {
    32  		path := strings.ReplaceAll(spec.Path.Value, "\"", "")
    33  		alias := ""
    34  		if spec.Name != nil && spec.Name.Name != "" {
    35  			alias = spec.Name.Name
    36  		}
    37  		v.Add(&Import{
    38  			Alias: alias,
    39  			Path:  path,
    40  		})
    41  	}
    42  	return
    43  }
    44  
    45  // Imports 一个fn文件一个,所以key不会重复,
    46  type Imports map[string]*Import
    47  
    48  func (s Imports) Find(ident string) (v *Import, has bool) {
    49  	v, has = s[ident]
    50  	return
    51  }
    52  
    53  func (s Imports) Path(path string) (v *Import, has bool) {
    54  	for _, i := range s {
    55  		if i.Path == path {
    56  			v = i
    57  			has = true
    58  			return
    59  		}
    60  	}
    61  	return
    62  }
    63  
    64  func (s Imports) Len() (n int) {
    65  	n = len(s)
    66  	return
    67  }
    68  
    69  func (s Imports) Add(i *Import) {
    70  	_, has := s.Find(i.Ident())
    71  	if !has {
    72  		s[i.Ident()] = i
    73  		return
    74  	}
    75  	return
    76  }
    77  
    78  type Import struct {
    79  	Path  string
    80  	Alias string
    81  }
    82  
    83  func (i *Import) Ident() (ident string) {
    84  	if i.Alias != "" {
    85  		ident = i.Alias
    86  		return
    87  	}
    88  	ident = i.Name()
    89  	return
    90  }
    91  
    92  func (i *Import) Name() (name string) {
    93  	idx := strings.LastIndex(i.Path, "/")
    94  	if idx < 0 {
    95  		name = i.Path
    96  	} else {
    97  		name = i.Path[idx+1:]
    98  	}
    99  	return
   100  }
   101  
   102  // MergeImports 在service里增加fn的imports用
   103  func MergeImports(ss []Imports) (v Imports) {
   104  	idents := make(map[string]int)
   105  	v = make(map[string]*Import)
   106  	for _, s := range ss {
   107  		for _, i := range s {
   108  			_, has := v.Path(i.Path)
   109  			if has {
   110  				continue
   111  			}
   112  			vv := &Import{
   113  				Path:  i.Path,
   114  				Alias: "",
   115  			}
   116  			_, hasIdent := v.Find(vv.Name())
   117  			if hasIdent {
   118  				times, hasIdents := idents[vv.Ident()]
   119  				if hasIdents {
   120  					times++
   121  				}
   122  				vv.Alias = fmt.Sprintf("%s%d", vv.Name(), times)
   123  				idents[vv.Name()] = times
   124  			}
   125  			v.Add(vv)
   126  		}
   127  	}
   128  	return
   129  }