-
Notifications
You must be signed in to change notification settings - Fork 25
/
unexported.go
109 lines (103 loc) · 3.15 KB
/
unexported.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"go/ast"
"log"
"unicode"
"unicode/utf8"
)
// Note: the code in this file is borrowed from the Go project.
// It is licensed under a BSD-style license that is available
// at https://golang.org/LICENSE.
//
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// trimUnexportedElems modifies spec in place to elide unexported fields from
// structs and methods from interfaces (unless the unexported flag is set).
func trimUnexportedElems(spec *ast.TypeSpec) {
switch typ := spec.Type.(type) {
case *ast.StructType:
typ.Fields = trimUnexportedFields(typ.Fields, false)
case *ast.InterfaceType:
typ.Methods = trimUnexportedFields(typ.Methods, true)
}
}
// trimUnexportedFields returns the field list trimmed of unexported fields.
func trimUnexportedFields(fields *ast.FieldList, isInterface bool) *ast.FieldList {
what := "methods"
if !isInterface {
what = "fields"
}
trimmed := false
list := make([]*ast.Field, 0, len(fields.List))
for _, field := range fields.List {
names := field.Names
if len(names) == 0 {
// Embedded type. Use the name of the type. It must be of type ident or *ident.
// Nothing else is allowed.
switch ident := field.Type.(type) {
case *ast.Ident:
if isInterface && ident.Name == "error" && ident.Obj == nil {
// For documentation purposes, we consider the builtin error
// type special when embedded in an interface, such that it
// always gets shown publicly.
list = append(list, field)
continue
}
names = []*ast.Ident{ident}
case *ast.StarExpr:
// Must have the form *identifier.
// This is only valid on embedded types in structs.
if ident, ok := ident.X.(*ast.Ident); ok && !isInterface {
names = []*ast.Ident{ident}
}
case *ast.SelectorExpr:
// An embedded type may refer to a type in another package.
names = []*ast.Ident{ident.Sel}
}
if names == nil {
// Can only happen if AST is incorrect. Safe to continue with a nil list.
log.Print("invalid program: unexpected type for embedded field")
}
}
// Trims if any is unexported. Good enough in practice.
ok := true
for _, name := range names {
if !isUpper(name.Name) {
trimmed = true
ok = false
break
}
}
if ok {
list = append(list, field)
}
}
if !trimmed {
return fields
}
unexportedField := &ast.Field{
Type: &ast.Ident{
// Hack: printer will treat this as a field with a named type.
// Setting Name and NamePos to ("", fields.Closing-1) ensures that
// when Pos and End are called on this field, they return the
// position right before closing '}' character.
Name: "",
NamePos: fields.Closing - 1,
},
Comment: &ast.CommentGroup{
List: []*ast.Comment{{Text: fmt.Sprintf("// Has unexported %s.\n", what)}},
},
}
return &ast.FieldList{
Opening: fields.Opening,
List: append(list, unexportedField),
Closing: fields.Closing,
}
}
// isUpper reports whether the name starts with an upper case letter.
func isUpper(name string) bool {
ch, _ := utf8.DecodeRuneInString(name)
return unicode.IsUpper(ch)
}