Skip to content

Conversation

@alexandre-daubois
Copy link
Member

The DX is currently not optimal as we need to touch and edit the original source file. It is still in WIP, but this PR aims to improve a lot the DX of the extension generator by making it idempotent.

The proposed approach is to generate a wrapper to the original source file that will call it and then make the bindings to C. The original Go source will remain untouched and the generator can be run multiple times easily.

This also adds a header comment to all generated files (C file, C headers et al.):

package ext

// AUTOGENERATED FILE - DO NOT EDIT.
//
// This file has been automatically generated by FrankenPHP extension generator
// and should not be edited as it will be overwritten when running the
// extension generator again.
//
// You may edit the file and remove this comment if you plan to manually maintain
// this file going forward.

// #include <stdlib.h>
// #include "ext.h"
import "C"

// rest of the file...

Comment on lines +145 to +151
start := 0
funcBytes := []byte(goFunction)
if idx := bytes.Index(funcBytes, []byte("func ")); idx != -1 {
start = idx + len("func ")
} else {
return ""
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
start := 0
funcBytes := []byte(goFunction)
if idx := bytes.Index(funcBytes, []byte("func ")); idx != -1 {
start = idx + len("func ")
} else {
return ""
}
idx := strings.Index(goFunction, "func ")
if idx == -1 {
return ""
}
start := idx + len("func ")

return ""
}

return string(bytes.TrimSpace(funcBytes[start:end]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return string(bytes.TrimSpace(funcBytes[start:end]))
return strings.TrimSpace(goFunction[start:end]))


// extractGoFunctionSignatureParams extracts the parameters from a Go function signature.
func extractGoFunctionSignatureParams(goFunction string) string {
start := bytes.IndexByte([]byte(goFunction), '(')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
start := bytes.IndexByte([]byte(goFunction), '(')
start := strings.IndexByte(goFunction, '(')

return ""
}

return string(bytes.TrimSpace([]byte(goFunction[start:end])))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return string(bytes.TrimSpace([]byte(goFunction[start:end])))
return strings.TrimSpace(goFunction[start:end]))


// extractGoFunctionSignatureReturn extracts the return type from a Go function signature.
func extractGoFunctionSignatureReturn(goFunction string) string {
start := bytes.IndexByte([]byte(goFunction), '(')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
start := bytes.IndexByte([]byte(goFunction), '(')
start := strings.IndexByte(goFunction, '(')

}

var names []string
parts := bytes.Split([]byte(params), []byte(","))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parts := bytes.Split([]byte(params), []byte(","))
parts := strings.Split(params, ",")

var names []string
parts := bytes.Split([]byte(params), []byte(","))
for _, part := range parts {
part = bytes.TrimSpace(part)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
part = bytes.TrimSpace(part)
part = strings.TrimSpace(part)

continue
}

words := bytes.Fields(part)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
words := bytes.Fields(part)
words := strings.Fields(part)


words := bytes.Fields(part)
if len(words) > 0 {
names = append(names, string(words[0]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
names = append(names, string(words[0]))
names = append(names, words[0])

Comment on lines +248 to +256
var result []byte
for i, name := range names {
if i > 0 {
result = append(result, []byte(", ")...)
}
result = append(result, []byte(name)...)
}

return string(result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var result []byte
for i, name := range names {
if i > 0 {
result = append(result, []byte(", ")...)
}
result = append(result, []byte(name)...)
}
return string(result)
var result strings.Builder
for i, name := range names {
if i > 0 {
result.WriteString(", ")
}
result.WriteString(name)
}
return result.String()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants