Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit descriptions as block strings in printer.Print #485

Merged
merged 5 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 136 additions & 6 deletions language/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ func getMapValueString(m map[string]interface{}, key string) string {
}
return ""
}
func getDescription(raw interface{}) string {
var desc string

switch node := raw.(type) {
case ast.DescribableNode:
if sval := node.GetDescription(); sval != nil {
desc = sval.Value
}
case map[string]interface{}:
desc = getMapValueString(node, "Description.Value")
}
if desc != "" {
sep := ""
if strings.ContainsRune(desc, '\n') {
sep = "\n"
}
desc = join([]string{`"""`, desc, `"""`}, sep)
}
return desc
}

func toSliceString(slice interface{}) []string {
if slice == nil {
Expand Down Expand Up @@ -506,6 +526,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
fmt.Sprintf("%v", node.Name),
join(directives, " "),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
Expand All @@ -518,6 +541,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
name,
join(directives, " "),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
Expand All @@ -539,6 +565,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
join(directives, " "),
block(fields),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
Expand All @@ -555,6 +584,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
join(directives, " "),
block(fields),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
Expand All @@ -569,7 +601,23 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
for _, directive := range node.Directives {
directives = append(directives, fmt.Sprintf("%v", directive.Name))
}
str := name + wrap("(", join(args, ", "), ")") + ": " + ttype + wrap(" ", join(directives, " "), "")
hasArgDesc := false
for _, arg := range node.Arguments {
if arg.Description != nil && arg.Description.Value != "" {
hasArgDesc = true
break
}
}
var argsStr string
if hasArgDesc {
argsStr = wrap("(", indent("\n"+join(args, "\n")), "\n)")
} else {
argsStr = wrap("(", join(args, ", "), ")")
}
str := name + argsStr + ": " + ttype + wrap(" ", join(directives, " "), "")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("\n%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
Expand All @@ -579,7 +627,23 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
for _, directive := range getMapSliceValue(node, "Directives") {
directives = append(directives, fmt.Sprintf("%v", directive))
}
str := name + wrap("(", join(args, ", "), ")") + ": " + ttype + wrap(" ", join(directives, " "), "")
hasArgDesc := false
for _, arg := range args {
if strings.HasPrefix(strings.TrimSpace(arg), `"""`) {
hasArgDesc = true
break
}
}
var argsStr string
if hasArgDesc {
argsStr = wrap("(", indent("\n"+join(args, "\n")), "\n)")
} else {
argsStr = wrap("(", join(args, ", "), ")")
}
str := name + argsStr + ": " + ttype + wrap(" ", join(directives, " "), "")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("\n%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
Expand All @@ -599,7 +663,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
wrap("= ", defaultValue, ""),
join(directives, " "),
}, " ")

if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("\n%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
Expand All @@ -614,6 +680,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
wrap("= ", defaultValue, ""),
join(directives, " "),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("\n%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
Expand All @@ -633,6 +702,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
join(directives, " "),
block(fields),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
Expand All @@ -647,6 +719,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
join(directives, " "),
block(fields),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
Expand All @@ -666,6 +741,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
join(directives, " "),
"= " + join(types, " | "),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
Expand All @@ -680,6 +758,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
join(directives, " "),
"= " + join(types, " | "),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
Expand All @@ -699,6 +780,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
join(directives, " "),
block(values),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
Expand All @@ -713,6 +797,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
join(directives, " "),
block(values),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
Expand All @@ -729,6 +816,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
name,
join(directives, " "),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("\n%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
Expand All @@ -740,6 +830,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
name,
join(directives, " "),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("\n%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
Expand All @@ -759,6 +852,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
join(directives, " "),
block(fields),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
Expand All @@ -773,6 +869,9 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
join(directives, " "),
block(fields),
}, " ")
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
Expand All @@ -793,15 +892,46 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
"DirectiveDefinition": func(p visitor.VisitFuncParams) (string, interface{}) {
switch node := p.Node.(type) {
case *ast.DirectiveDefinition:
args := wrap("(", join(toSliceString(node.Arguments), ", "), ")")
str := fmt.Sprintf("directive @%v%v on %v", node.Name, args, join(toSliceString(node.Locations), " | "))
args := toSliceString(node.Arguments)
hasArgDesc := false
for _, arg := range node.Arguments {
if arg.Description != nil && arg.Description.Value != "" {
hasArgDesc = true
break
}
}
var argsStr string
if hasArgDesc {
argsStr = wrap("(", indent("\n"+join(args, "\n")), "\n)")
} else {
argsStr = wrap("(", join(args, ", "), ")")
}
str := fmt.Sprintf("directive @%v%v on %v", node.Name, argsStr, join(toSliceString(node.Locations), " | "))
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
case map[string]interface{}:
name := getMapValueString(node, "Name")
locations := toSliceString(getMapValue(node, "Locations"))
args := toSliceString(getMapValue(node, "Arguments"))
argsStr := wrap("(", join(args, ", "), ")")
hasArgDesc := false
for _, arg := range args {
if strings.HasPrefix(strings.TrimSpace(arg), `"""`) {
hasArgDesc = true
break
}
}
var argsStr string
if hasArgDesc {
argsStr = wrap("(", indent("\n"+join(args, "\n")), "\n)")
} else {
argsStr = wrap("(", join(args, ", "), ")")
}
str := fmt.Sprintf("directive @%v%v on %v", name, argsStr, join(locations, " | "))
if desc := getDescription(node); desc != "" {
str = fmt.Sprintf("%s\n%s", desc, str)
}
return visitor.ActionUpdate, str
}
return visitor.ActionNoChange, nil
Expand Down
Loading