Skip to content

Commit

Permalink
Fixing golang lint, cleaning up golang generator to make it more stable
Browse files Browse the repository at this point in the history
Signed-off-by: Shivansh Vij <shivanshvij@loopholelabs.io>
  • Loading branch information
ShivanshVij committed Sep 20, 2023
1 parent f2404ba commit 9b986ef
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
13 changes: 8 additions & 5 deletions build/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,15 @@ func LocalTypescript(options *LocalTypescriptOptions) (*scalefunc.Schema, error)
return nil, fmt.Errorf("unable to parse signature dependency in package.json")
}

signatureVersion := ""
if !strings.HasPrefix(signaturePath, "file:") {
signatureVersion = signaturePath
signatureImport := ""
switch {
case strings.HasPrefix(signaturePath, "http://") || strings.HasPrefix(signaturePath, "https://"):
signatureImport = signaturePath
signaturePath = ""
} else {
case strings.HasPrefix(signaturePath, "file:"):
signaturePath = strings.TrimPrefix(signaturePath, "file:")
default:
return nil, fmt.Errorf("unable to parse signature dependency path: %s", signaturePath)
}

if signaturePath == "" && options.Scalefile.Signature.Organization == "local" {
Expand Down Expand Up @@ -197,7 +200,7 @@ func LocalTypescript(options *LocalTypescriptOptions) (*scalefunc.Schema, error)
return nil, fmt.Errorf("unable to write index.js file for function dist: %w", err)
}

packageJSONFile, err := typescript.GenerateTypescriptPackageJSON(options.Scalefile, signaturePath, signatureVersion)
packageJSONFile, err := typescript.GenerateTypescriptPackageJSON(options.Scalefile, signaturePath, signatureImport)
if err != nil {
return nil, fmt.Errorf("unable to generate package.json file: %w", err)
}
Expand Down
12 changes: 6 additions & 6 deletions compile/typescript/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (

var generator *Generator

func GenerateTypescriptPackageJSON(packageSchema *scalefile.Schema, signaturePath string, signatureVersion string) ([]byte, error) {
return generator.GenerateTypescriptPackageJSON(packageSchema, signaturePath, signatureVersion)
func GenerateTypescriptPackageJSON(packageSchema *scalefile.Schema, signaturePath string, signatureImport string) ([]byte, error) {
return generator.GenerateTypescriptPackageJSON(packageSchema, signaturePath, signatureImport)
}

func GenerateTypescriptIndex(packageSchema *scalefile.Schema, functionPath string) ([]byte, error) {
Expand All @@ -48,16 +48,16 @@ func New() *Generator {
}
}

func (g *Generator) GenerateTypescriptPackageJSON(packageSchema *scalefile.Schema, signaturePath string, signatureVersion string) ([]byte, error) {
func (g *Generator) GenerateTypescriptPackageJSON(packageSchema *scalefile.Schema, signaturePath string, signatureImport string) ([]byte, error) {
if signaturePath != "" && !strings.HasPrefix(signaturePath, "/") && !strings.HasPrefix(signaturePath, "./") && !strings.HasPrefix(signaturePath, "../") {
signaturePath = "./" + signaturePath
}

buf := new(bytes.Buffer)
err := g.template.ExecuteTemplate(buf, "packagejson.ts.templ", map[string]interface{}{
"package_schema": packageSchema,
"signature_path": signaturePath,
"signature_version": signatureVersion,
"package_schema": packageSchema,
"signature_path": signaturePath,
"signature_import": signatureImport,
})
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions compile/typescript/templates/packagejson.ts.templ
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
{{ if .signature_path }}
"signature": "file:{{ .signature_path}}"
{{ end }}
{{ if .signature_version }}
"signature": "{{ .signature_version }}"
{{ if .signature_import }}
"signature": "{{ .signature_import }}"
{{ end }}
}
}
2 changes: 0 additions & 2 deletions integration/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func TestGenerateMasterTestingSchema(t *testing.T) {
Signature: s,

GolangPackageImportPath: "signature",
GolangPackageName: "signature",
GolangPackageVersion: "v0.1.0",

RustPackageName: "local_example_latest_guest",
Expand Down Expand Up @@ -90,7 +89,6 @@ func TestGenerateMasterTestingSchema(t *testing.T) {
Signature: s,

GolangPackageImportPath: "signature",
GolangPackageName: "signature",
GolangPackageVersion: "v0.1.0",

TypescriptPackageName: "local-example-latest-host",
Expand Down
21 changes: 12 additions & 9 deletions signature/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import (
"github.com/loopholelabs/scale/signature/generator/rust"
)

const (
DefaultGolangPackageName = "signature"
)

type GuestRegistryPackage struct {
GolangModule *bytes.Buffer
GolangModfile []byte
Expand Down Expand Up @@ -63,7 +67,6 @@ type Options struct {
Signature *signature.Schema

GolangPackageImportPath string
GolangPackageName string
GolangPackageVersion string

RustPackageName string
Expand All @@ -80,12 +83,12 @@ func GenerateGuestRegistry(options *Options) (*GuestRegistryPackage, error) {
}
hashString := hex.EncodeToString(hash)

golangTypes, err := golang.GenerateTypes(options.Signature, options.GolangPackageName)
golangTypes, err := golang.GenerateTypes(options.Signature, DefaultGolangPackageName)
if err != nil {
return nil, err
}

golangGuest, err := golang.GenerateGuest(options.Signature, hashString, options.GolangPackageName)
golangGuest, err := golang.GenerateGuest(options.Signature, hashString, DefaultGolangPackageName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -252,12 +255,12 @@ func GenerateGuestLocal(options *Options) (*GuestLocalPackage, error) {
}
hashString := hex.EncodeToString(hash)

golangTypes, err := golang.GenerateTypes(options.Signature, options.GolangPackageName)
golangTypes, err := golang.GenerateTypes(options.Signature, DefaultGolangPackageName)
if err != nil {
return nil, err
}

golangGuest, err := golang.GenerateGuest(options.Signature, hashString, options.GolangPackageName)
golangGuest, err := golang.GenerateGuest(options.Signature, hashString, DefaultGolangPackageName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -340,12 +343,12 @@ func GenerateHostRegistry(options *Options) (*HostRegistryPackage, error) {
return nil, err
}

golangTypes, err := golang.GenerateTypes(sig, options.GolangPackageName)
golangTypes, err := golang.GenerateTypes(sig, DefaultGolangPackageName)
if err != nil {
return nil, err
}

golangHost, err := golang.GenerateHost(sig, hashString, options.GolangPackageName)
golangHost, err := golang.GenerateHost(sig, hashString, DefaultGolangPackageName)

if err != nil {
return nil, err
Expand Down Expand Up @@ -457,12 +460,12 @@ func GenerateHostLocal(options *Options) (*HostLocalPackage, error) {
return nil, err
}

golangTypes, err := golang.GenerateTypes(sig, options.GolangPackageName)
golangTypes, err := golang.GenerateTypes(sig, DefaultGolangPackageName)
if err != nil {
return nil, err
}

golangHost, err := golang.GenerateHost(sig, hashString, options.GolangPackageName)
golangHost, err := golang.GenerateHost(sig, hashString, DefaultGolangPackageName)
if err != nil {
return nil, err
}
Expand Down
2 changes: 0 additions & 2 deletions storage/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ func GenerateSignature(sig *signature.Schema, name string, tag string, org strin
Signature: sig,

GolangPackageImportPath: "signature",
GolangPackageName: "signature",
GolangPackageVersion: "v0.1.0",

RustPackageName: fmt.Sprintf("%s_%s_%s_guest", org, name, tag),
Expand All @@ -338,7 +337,6 @@ func GenerateSignature(sig *signature.Schema, name string, tag string, org strin
hostPackage, err := generator.GenerateHostLocal(&generator.Options{
Signature: sig,
GolangPackageImportPath: "signature",
GolangPackageName: "signature",
GolangPackageVersion: "v0.1.0",
})
if err != nil {
Expand Down

0 comments on commit 9b986ef

Please sign in to comment.