Skip to content

Commit

Permalink
Updated setters generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mymmrac committed Feb 25, 2023
1 parent 39ba208 commit cb0d752
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
14 changes: 12 additions & 2 deletions internal/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,23 @@ func docsText() (string, error) {
}

func formatFile(filename string) {
buf := bytes.Buffer{}

cmd := exec.Command("goimports", "-w", filename)

buf := bytes.Buffer{}
cmd.Stderr = &buf

if err := cmd.Run(); err != nil {
logError("Gofmt: %v\n%s", err, buf.String())
os.Exit(1)
}

cmd = exec.Command("gofumpt", "-w", filename)

buf = bytes.Buffer{}
cmd.Stderr = &buf

if err := cmd.Run(); err != nil {
logError("Gofumpt: %v\n%s", err, buf.String())
os.Exit(1)
}
}
16 changes: 9 additions & 7 deletions internal/generator/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func generateSetters(typesData string, desiredStructs []string) tgSetters {
}

if (strings.HasPrefix(setter.structType, "InlineQueryResult") ||
strings.HasPrefix(setter.structType, "InputMedia")) &&
strings.HasPrefix(setter.structType, "InputMedia") ||
setter.structType == "MenuButtonWebApp") &&
setter.fieldName == "Type" {
continue
}
Expand Down Expand Up @@ -106,19 +107,20 @@ func writeSetters(file *os.File, setters tgSetters, receiverDefault bool, noPoin

noPointer := contains(noPointerStructs, setter.structType)

boolPtr := setter.fieldType == "*bool" && setter.structType == "PromoteChatMemberParams"
convertToPtr := setter.fieldType == "*bool" && setter.structType == "PromoteChatMemberParams" ||
setter.fieldType == "*string" && setter.structType == "InlineKeyboardButton"

var s string
if setter.fieldType != "bool" {
if boolPtr {
setter.fieldType = "bool"
if convertToPtr {
setter.fieldType = strings.TrimPrefix(setter.fieldType, "*")
}

s = fmt.Sprintf("func (%s *%s) With%s(%s %s) *%s {\n", r, setter.structType,
setter.fieldName, firstToLower(setter.fieldName), setter.fieldType, setter.structType)

if boolPtr {
setter.fieldType = "*bool"
if convertToPtr {
setter.fieldType = "*" + setter.fieldType
}
} else {
s = fmt.Sprintf("func (%s *%s) With%s() *%s {\n", r, setter.structType,
Expand All @@ -138,7 +140,7 @@ func writeSetters(file *os.File, setters tgSetters, receiverDefault bool, noPoin

if setter.fieldType != "bool" {
value := firstToLower(setter.fieldName)
if boolPtr {
if convertToPtr {
value = "ToPtr(" + value + ")"
}

Expand Down

0 comments on commit cb0d752

Please sign in to comment.