Skip to content

Commit

Permalink
Update code generation comments, Add support for multiple schema sour…
Browse files Browse the repository at this point in the history
…ces, fix missing null type
  • Loading branch information
AmarnathCJD committed Apr 1, 2024
1 parent 8817ad3 commit 3df682f
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 30 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/tlgen/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (g *Generator) Generate() error {

func (*Generator) generateFile(f func(file *jen.File), filename string) error {
file := jen.NewFile("telegram")
file.HeaderComment("Code generated by generate-tl-files; DO NOT EDIT.")
file.HeaderComment("Code generated by TLParser; DO NOT EDIT. (c) @amarnathcjd")
f(file)

buf := bytes.NewBuffer([]byte{})
Expand Down
110 changes: 95 additions & 15 deletions internal/cmd/tlgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"

Expand All @@ -15,9 +16,14 @@ import (
)

const (
API_SOURCE = "https://raw.githubusercontent.com/null-nick/TL-Schema/c3e09f4310413ff49b695dfe78df64044153c905/api.tl" // "https://raw.githubusercontent.com/telegramdesktop/tdesktop/dev/Telegram/SourceFiles/mtproto/scheme/api.tl"
tlLOC = "../../../schemes/api.tl"
desLOC = "../../../telegram/"
tlLOC = "../../../schemes/api.tl"
desLOC = "../../../telegram/"
)

var (
API_SOURCES = []string{"https://raw.githubusercontent.com/null-nick/TL-Schema/c3e09f4310413ff49b695dfe78df64044153c905/api.tl",
"https://raw.githubusercontent.com/telegramdesktop/tdesktop/dev/Telegram/SourceFiles/mtproto/scheme/api.tl",
}
)

const helpMsg = `welcome to gogram's TL generator (c) @amarnathcjd`
Expand All @@ -35,21 +41,12 @@ func main() {
llayer := reg.FindString(str)
llayer = strings.TrimPrefix(llayer, "ApiVersion = ")

currentRemoteAPIVersion, err := http.Get(API_SOURCE)
remoteAPIVersion, rlayer, err := getSourceLAYER(llayer)
if err != nil {
panic(err)
fmt.Println(err)
return
}

remoteAPIVersion, err := io.ReadAll(currentRemoteAPIVersion.Body)
if err != nil {
panic(err)
}

reg = regexp.MustCompile(`// LAYER \d+`)
str = string(remoteAPIVersion)
rlayer := reg.FindString(str)
rlayer = strings.TrimPrefix(rlayer, "// LAYER ")

if !strings.EqualFold(llayer, rlayer) {
fmt.Println("Local API version is", llayer, "and remote API version is", rlayer)
fmt.Println("Performing update")
Expand Down Expand Up @@ -90,6 +87,48 @@ func main() {
}
}

func getSourceLAYER(llayer string) ([]byte, string, error) {
reg := regexp.MustCompile(`// LAYER \d+`)

for _, source := range API_SOURCES {
src, err := http.Get(source)
if err != nil {
return nil, "", err
}

remoteAPIVersion, err := io.ReadAll(src.Body)
if err != nil {
return nil, "", err
}

rlayer := reg.FindString(string(remoteAPIVersion))
rlayer = strings.TrimPrefix(rlayer, "// LAYER ")

if !strings.EqualFold(llayer, rlayer) {
rlayer_int, err := strconv.Atoi(rlayer)
if err != nil {
return nil, "", err
}

llayer_int, err := strconv.Atoi(llayer)
if err != nil {
return nil, "", err
}

if rlayer_int > llayer_int {
return remoteAPIVersion, rlayer, nil
}
} else {
fmt.Println("Skipping (<=) ~ Source [", source, "] ("+rlayer+")")
continue
}

return remoteAPIVersion, rlayer, fmt.Errorf("No update required (Local API version is %s and remote API (TDesktop) version is %s)", llayer, rlayer)
}

return nil, "", fmt.Errorf("No update required ~")
}

func root(tlfile, outdir string) error {
startTime := time.Now()
b, err := os.ReadFile(tlfile)
Expand Down Expand Up @@ -275,9 +314,14 @@ func cleanComments(b []byte) []byte {
lines := strings.Split(string(b), "\n")
var clean []string

var parsedManually bool

for _, line := range lines {
if strings.HasPrefix(line, "//") && !strings.Contains(line, "////") {
if strings.Contains(line, "Not used") || strings.Contains(line, "Parsed manually") || strings.Contains(line, "https://") {
if strings.Contains(line, "Parsed manually") {
parsedManually = true
}
continue
}
} else if strings.Contains(line, "////") || strings.Contains(line, "{X:Type}") {
Expand All @@ -287,5 +331,41 @@ func cleanComments(b []byte) []byte {
clean = append(clean, line)
}

// replace consecutive 2+ newlines with single newline

b = []byte(strings.Join(clean, "\n"))
lines = strings.Split(string(b), "\n")

clean = []string{}
for i := 0; i < len(lines); i++ {
if i+1 < len(lines) && len(lines[i]) == 0 && len(lines[i+1]) == 0 {
continue
}

clean = append(clean, lines[i])
}

b = []byte(strings.Join(clean, "\n"))

// add some bytes to its start

if parsedManually {

clean = []string{`boolFalse#bc799737 = Bool;
boolTrue#997275b5 = Bool;
true#3fedd339 = True;
vector#1cb5c415 {t:Type} # [ t ] = Vector t;
error#c4b9f9bb code:int text:string = Error;
null#56730bcc = Null;`}
} else {
clean = []string{}
}

clean = append(clean, string(b))

return []byte(strings.Join(clean, "\n"))
}
17 changes: 10 additions & 7 deletions schemes/api.tl
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@

boolFalse#bc799737 = Bool;
boolTrue#997275b5 = Bool;

true#3fedd339 = True;

vector#1cb5c415 {t:Type} # [ t ] = Vector t;

error#c4b9f9bb code:int text:string = Error;

null#56730bcc = Null;

---types---






inputPeerEmpty#7f3b18ea = InputPeer;
inputPeerSelf#7da07ec9 = InputPeer;
inputPeerChat#35a95cb9 chat_id:long = InputPeer;
Expand Down Expand Up @@ -1770,7 +1774,6 @@ stats.broadcastRevenueTransactions#87158466 count:int transactions:Vector<Broadc

---functions---


auth.sendCode#a677244f phone_number:string api_id:int api_hash:string settings:CodeSettings = auth.SentCode;
auth.signUp#aac7b717 flags:# no_joined_notifications:flags.0?true phone_number:string phone_code_hash:string first_name:string last_name:string = auth.Authorization;
auth.signIn#8d52a951 flags:# phone_number:string phone_code_hash:string phone_code:flags.0?string email_verification:flags.1?EmailVerification = auth.Authorization;
Expand Down
19 changes: 18 additions & 1 deletion telegram/enums_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions telegram/init_gen.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion telegram/interfaces_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion telegram/methods_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion telegram/types_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3df682f

Please sign in to comment.