api imports take the form of relative paths (#3201)
Co-authored-by: 李春华 <lichunhua@threesoft.cn>
This commit is contained in:
@@ -19,7 +19,6 @@ type (
|
|||||||
linePrefix string
|
linePrefix string
|
||||||
debug bool
|
debug bool
|
||||||
log console.Console
|
log console.Console
|
||||||
src string
|
|
||||||
skipCheckTypeDeclaration bool
|
skipCheckTypeDeclaration bool
|
||||||
handlerMap map[string]PlaceHolder
|
handlerMap map[string]PlaceHolder
|
||||||
routeMap map[string]PlaceHolder
|
routeMap map[string]PlaceHolder
|
||||||
@@ -88,30 +87,29 @@ func (p *Parser) Parse(filename string) (*Api, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.src = abs
|
|
||||||
data, err := p.readContent(filename)
|
data, err := p.readContent(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.importStatck.push(p.src)
|
p.importStatck.push(abs)
|
||||||
return p.parse(filename, data)
|
return p.parse(filename, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseContent is used to parse the api from the specified content
|
// ParseContent is used to parse the api from the specified content
|
||||||
func (p *Parser) ParseContent(content string, filename ...string) (*Api, error) {
|
func (p *Parser) ParseContent(content string, filename ...string) (*Api, error) {
|
||||||
var f string
|
var f, abs string
|
||||||
if len(filename) > 0 {
|
if len(filename) > 0 {
|
||||||
f = filename[0]
|
f = filename[0]
|
||||||
abs, err := filepath.Abs(f)
|
a, err := filepath.Abs(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.src = abs
|
abs = a
|
||||||
}
|
}
|
||||||
|
|
||||||
p.importStatck.push(p.src)
|
p.importStatck.push(abs)
|
||||||
return p.parse(f, content)
|
return p.parse(f, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +125,7 @@ func (p *Parser) parse(filename, content string) (*Api, error) {
|
|||||||
apiAstList = append(apiAstList, root)
|
apiAstList = append(apiAstList, root)
|
||||||
p.storeVerificationInfo(root)
|
p.storeVerificationInfo(root)
|
||||||
p.syntax = root.Syntax
|
p.syntax = root.Syntax
|
||||||
impApiAstList, err := p.invokeImportedApi(root.Import)
|
impApiAstList, err := p.invokeImportedApi(filename, root.Import)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -144,10 +142,10 @@ func (p *Parser) parse(filename, content string) (*Api, error) {
|
|||||||
return allApi, nil
|
return allApi, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) invokeImportedApi(imports []*ImportExpr) ([]*Api, error) {
|
func (p *Parser) invokeImportedApi(filename string, imports []*ImportExpr) ([]*Api, error) {
|
||||||
var apiAstList []*Api
|
var apiAstList []*Api
|
||||||
for _, imp := range imports {
|
for _, imp := range imports {
|
||||||
dir := filepath.Dir(p.src)
|
dir := filepath.Dir(filename)
|
||||||
impPath := strings.ReplaceAll(imp.Value.Text(), "\"", "")
|
impPath := strings.ReplaceAll(imp.Value.Text(), "\"", "")
|
||||||
if !filepath.IsAbs(impPath) {
|
if !filepath.IsAbs(impPath) {
|
||||||
impPath = filepath.Join(dir, impPath)
|
impPath = filepath.Join(dir, impPath)
|
||||||
@@ -178,7 +176,7 @@ func (p *Parser) invokeImportedApi(imports []*ImportExpr) ([]*Api, error) {
|
|||||||
}
|
}
|
||||||
p.storeVerificationInfo(nestedApi)
|
p.storeVerificationInfo(nestedApi)
|
||||||
apiAstList = append(apiAstList, nestedApi)
|
apiAstList = append(apiAstList, nestedApi)
|
||||||
list, err := p.invokeImportedApi(nestedApi.Import)
|
list, err := p.invokeImportedApi(impPath, nestedApi.Import)
|
||||||
p.importStatck.pop()
|
p.importStatck.pop()
|
||||||
apiAstList = append(apiAstList, list...)
|
apiAstList = append(apiAstList, list...)
|
||||||
|
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ func primitiveType(tp string) (string, bool) {
|
|||||||
switch tp {
|
switch tp {
|
||||||
case "string":
|
case "string":
|
||||||
return "string", true
|
return "string", true
|
||||||
case "int", "int8", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
|
case "int", "int8","int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
|
||||||
return "number", true
|
return "number", true
|
||||||
case "float", "float32", "float64":
|
case "float", "float32", "float64":
|
||||||
return "number", true
|
return "number", true
|
||||||
|
|||||||
Reference in New Issue
Block a user