Skip to content

Commit

Permalink
add uppercase getter
Browse files Browse the repository at this point in the history
  • Loading branch information
mobiletoly committed Dec 7, 2021
1 parent f0f93a9 commit 5aab923
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
3 changes: 2 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
)

type Person struct {
type Person struct { //+gob:constructor
firstName, lastName *string //+gob:getter
Age int `json:"age"`
Description *string `json:"description"` //+gob:_
Expand All @@ -14,6 +14,7 @@ type Person struct {
test strings.Builder //+gob:getter
test2 *ast.Scope //+gob:getter
test3 *map[string]interface{}
xml *string //+gob:GETTER

anotherPerson *anotherPerson
}
Expand Down
41 changes: 27 additions & 14 deletions gobld.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type StructParser struct {
flagReceiverPtrRegexp *regexp.Regexp
flagOptionalRegexp *regexp.Regexp
flagGetterRegexp *regexp.Regexp
flagUppercaseGetterRegexp *regexp.Regexp
}

type GobBuilder struct {
Expand All @@ -41,8 +42,8 @@ type GobBuilder struct {

type StructFlags struct {
ProcessStruct bool
Visibility Visibility
PtrReceiver bool
Visibility Visibility
}

func (bld *GobBuilder) appendPackage() {
Expand Down Expand Up @@ -87,17 +88,16 @@ func newStructArgName(structName string, fieldName string, visibility Visibility
func convertStructNameAccordingToVisibility(structName string, visibility Visibility) string {
if visibility == PackageLevelVisibility {
return untitle(structName)
} else {
return structName
}
return structName
}

func untitle(value string) string {
return string(unicode.ToLower(rune(value[0]))) + value[1:]
}

func (bld *GobBuilder) appendBeginConstructorDef(structName string, structFlags StructFlags) {
var funcName = ""
var funcName string
firstChar := rune(structName[0])
if unicode.IsLower(firstChar) || structFlags.Visibility == PackageLevelVisibility {
funcName = "new" + strings.Title(structName)
Expand Down Expand Up @@ -125,14 +125,22 @@ func (bld *GobBuilder) appendConstructorArg(fieldName string, structArgName stri
bld.constructorPtrBody.WriteString(value)
}

func (bld *GobBuilder) appendGetter(structName string, fieldName string, fieldType string, flags StructFlags) {
func (bld *GobBuilder) appendGetter(
structName string, fieldName string, fieldType string, flags StructFlags, allUppercase bool,
) {
ptr := ""
if flags.PtrReceiver {
ptr = "*"
}
bld.common.WriteString(fmt.Sprintf("func (v %s%s) %s() %s {\n", ptr, structName, strings.Title(fieldName), fieldType))
var addedFieldName string
if allUppercase {
addedFieldName = strings.ToUpper(fieldName)
} else {
addedFieldName = strings.Title(fieldName)
}
bld.common.WriteString(fmt.Sprintf("func (v %s%s) %s() %s {\n", ptr, structName, addedFieldName, fieldType))
bld.common.WriteString(fmt.Sprintf("\treturn v.%s\n", fieldName))
bld.common.WriteString(fmt.Sprint("}\n\n"))
bld.common.WriteString("}\n\n")
}

func (bld *GobBuilder) Build() string {
Expand Down Expand Up @@ -170,12 +178,13 @@ func NewStructParser(fileSet *token.FileSet, fileContent []byte) StructParser {
fileSet: fileSet,
fileContent: fileContent,
whitespaceRegexp: regexp.MustCompile(`\s+`),
constructorExportedRegexp: regexp.MustCompile("\\b+gob:Constructor\\b"),
constructorPackageRegexp: regexp.MustCompile("\\b+gob:constructor\\b"),
constructorNoRegexp: regexp.MustCompile("\\b+gob:_\\b"),
flagReceiverPtrRegexp: regexp.MustCompile("\\b+gob:ptr\\b"),
flagOptionalRegexp: regexp.MustCompile("\\b+gob:_\\b"),
flagGetterRegexp: regexp.MustCompile("\\b+gob:getter\\b"),
constructorExportedRegexp: regexp.MustCompile(`\b+gob:Constructor\b`),
constructorPackageRegexp: regexp.MustCompile(`\b+gob:constructor\b`),
constructorNoRegexp: regexp.MustCompile(`\b+gob:_\b`),
flagReceiverPtrRegexp: regexp.MustCompile(`\b+gob:ptr\b`),
flagOptionalRegexp: regexp.MustCompile(`\b+gob:_\b`),
flagGetterRegexp: regexp.MustCompile(`\b+gob:getter\b`),
flagUppercaseGetterRegexp: regexp.MustCompile(`\b+gob:GETTER\b`),
}
}

Expand All @@ -193,15 +202,19 @@ func (sp *StructParser) fieldGetter(field *ast.Field) bool {
return sp.flagGetterRegexp.MatchString(field.Comment.Text())
}

func (sp *StructParser) fieldUppercaseGetter(field *ast.Field) bool {
return sp.flagUppercaseGetterRegexp.MatchString(field.Comment.Text())
}

func (sp *StructParser) constructorFlags(st *ast.StructType) StructFlags {
begin := st.Struct
endLine := sp.fileSet.File(begin).Line(begin) + 1
end := sp.fileSet.File(begin).LineStart(endLine)
result := string(sp.fileContent[sp.fileSet.Position(begin).Offset:sp.fileSet.Position(end).Offset])
flags := StructFlags{
ProcessStruct: false,
Visibility: ExportedVisibility,
PtrReceiver: false,
Visibility: ExportedVisibility,
}
if sp.constructorPackageRegexp.MatchString(result) {
flags.ProcessStruct = true
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ func main() {
}
}
if sp.fieldGetter(field) {
gobBld.appendGetter(structName, fieldName.Name, fieldTypeText, structFlags)
gobBld.appendGetter(structName, fieldName.Name, fieldTypeText, structFlags, false)
} else if sp.fieldUppercaseGetter(field) {
gobBld.appendGetter(structName, fieldName.Name, fieldTypeText, structFlags, true)
}
}
}
Expand Down

0 comments on commit 5aab923

Please sign in to comment.