Skip to content

Commit

Permalink
Wip multiple functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nolag committed Aug 12, 2024
1 parent af2f9b3 commit 07929cb
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 340 deletions.
73 changes: 44 additions & 29 deletions pkg/capabilities/cli/cmd/generate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/atombender/go-jsonschema/pkg/generator"
"github.com/atombender/go-jsonschema/pkg/schemas"
"github.com/iancoleman/strcase"
"github.com/spf13/cobra"

"github.com/smartcontractkit/chainlink-common/pkg/capabilities"
Expand All @@ -20,7 +21,7 @@ var Dir string

// CapabilitySchemaFilePattern is used to extract the package name from the file path.
// This is used as the package name for the generated Go types.
var CapabilitySchemaFilePattern = regexp.MustCompile(`([^/]+)_(action|trigger|consensus|target)-schema\.json$`)
var CapabilitySchemaFilePattern = regexp.MustCompile(`([^/]+)_(action|trigger|consensus|target|common)-schema\.json$`)

// reg := regexp.MustCompile(`([^/]+)_(trigger|action)\.json$`)

Expand Down Expand Up @@ -81,50 +82,58 @@ func GenerateTypes(dir string, helpers []WorkflowHelperGenerator) error {
}

var capabilityType capabilities.CapabilityType

for ; capabilityType.IsValid() == nil && (capabilityType.String() != capabilityTypeRaw); capabilityType++ {
}
if err = capabilityType.IsValid(); err != nil {
if err = capabilityType.IsValid(); err != nil && capabilityTypeRaw != "common" {
return fmt.Errorf("invalid capability type %v", capabilityTypeRaw)
}

allFiles[file] = content

abs, err := filepath.Abs(schemaPath)
if err != nil {
return err
if capabilityTypeRaw != "common" {
if err = genHelpers(schemaPath, ids, content, rootType, capabilityType, helpers, allFiles); err != nil {
return err
}
}

id := ids[schemaPath]
idParts := strings.Split(id, "/")
id = idParts[len(idParts)-1]
var capId *string
if strings.Contains(id, "@") {
capId = &id
}
structs, err := StructsFromSrc(path.Dir(abs), content, rootType, capId, capabilityType)
if err != nil {
if err = printFiles(path.Dir(schemaPath), allFiles); err != nil {
return err
}
}
return nil
}

for _, helper := range helpers {
files, err := helper.Generate(structs)
if err != nil {
return err
}
func genHelpers(schemaPath string, ids map[string]string, content string, rootType string, capabilityType capabilities.CapabilityType, helpers []WorkflowHelperGenerator, allFiles map[string]string) error {
abs, err := filepath.Abs(schemaPath)
if err != nil {
return err
}

for f, c := range files {
if _, ok := allFiles[f]; ok {
return fmt.Errorf("file %v is being created by more than one generator", f)
}
allFiles[f] = c
}
}
id := ids[schemaPath]
idParts := strings.Split(id, "/")
id = idParts[len(idParts)-1]
var capId *string
if strings.Contains(id, "@") {
capId = &id
}
structs, err := StructsFromSrc(path.Dir(abs), content, rootType, capId, capabilityType)
if err != nil {
return err
}

if err = printFiles(path.Dir(schemaPath), allFiles); err != nil {
for _, helper := range helpers {
files, err := helper.Generate(structs)
if err != nil {
return err
}

fmt.Println("Generated types for", schemaPath)
for f, c := range files {
if _, ok := allFiles[f]; ok {
return fmt.Errorf("file %v is being created by more than one generator", f)
}
allFiles[f] = c
}
}
return nil
}
Expand All @@ -146,7 +155,8 @@ func ConfigFromSchemas(schemaFilePaths []string) (generator.Config, map[string]s
}
capabilityType := capabilityInfo[2]
outputName := strings.Replace(schemaFilePath, capabilityType+"-schema.json", capabilityType+"_generated.go", 1)
rootType := capitalize(capabilityType)
functionName := strcase.ToCamel(strings.Join(strings.Split(capabilityInfo[1], "_")[1:], ")"))
rootType := functionName + capitalize(capabilityType)
ids[schemaFilePath] = jsonSchema.ID

cfg.SchemaMappings = append(cfg.SchemaMappings, generator.SchemaMapping{
Expand Down Expand Up @@ -186,3 +196,8 @@ func TypesFromJSONSchema(schemaFilePath string, cfg generator.Config) (outputFil
func capitalize(s string) string {
return strings.ToUpper(string(s[0])) + s[1:]
}

type schemaPathAndGenExtra struct {
schemaPath string
genExtra bool
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"text/template"

"github.com/iancoleman/strcase"

"github.com/smartcontractkit/chainlink-common/pkg/capabilities"
)

//go:embed templates/go_workflow_builder.go.tmpl
Expand Down Expand Up @@ -76,6 +78,9 @@ func genFromTemplate(name, rawTemplate string, info GeneratedInfo) (string, erro
"HasOutputs": func(tpe string) bool {
return len(info.Types[tpe].Outputs) > 0
},
"IsCommon": func(tpe capabilities.CapabilityType) bool {
return tpe.IsValid() != nil
},
}).Parse(rawTemplate)
if err != nil {
return "", err
Expand Down
29 changes: 29 additions & 0 deletions pkg/capabilities/consensus/ocr3/ocr3_common-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/smartcontractkit/chainlink-common/pkg/capabilities/consensus/ocr3/common",
"$defs": {
"SignedReport": {
"type": "object",
"properties": {
"WorkflowExecutionID": {
"type": "string"
},
"Value": {
"type": "object",
"properties": {
"Underlying": {
"type": "object"
}
},
"additionalProperties": false,
"required": ["Underlying"]
},
"Err": {
"type": "boolean"
}
},
"additionalProperties": false,
"required": ["WorkflowExecutionID", "Value", "Err"]
}
}
}
66 changes: 66 additions & 0 deletions pkg/capabilities/consensus/ocr3/ocr3_common_generated.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,6 @@
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/smartcontractkit/chainlink-common/pkg/capabilities/consensus/ocr3/[email protected]",
"$defs": {
"SignedReport": {
"type": "object",
"properties": {
"WorkflowExecutionID": {
"type": "string"
},
"Value": {
"type": "object",
"properties": {
"Underlying": {
"type": "object"
}
},
"additionalProperties": false,
"required": ["Underlying"]
},
"Err": {
"type": "boolean"
}
},
"additionalProperties": false,
"required": ["WorkflowExecutionID", "Value", "Err"]
},
"FeedValue": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -121,7 +98,7 @@
"required": ["observations"]
},
"outputs": {
"$ref": "#/$defs/SignedReport"
"$ref": "ocr3_common-schema.json#/$defs/SignedReport"
}
},
"additionalProperties": false,
Expand Down
Loading

0 comments on commit 07929cb

Please sign in to comment.