Skip to content

Commit

Permalink
cmd/mkunion: now supports mkmatch tag
Browse files Browse the repository at this point in the history
  • Loading branch information
widmogrod committed Jul 13, 2024
1 parent 5362a13 commit c75ecdc
Showing 1 changed file with 52 additions and 57 deletions.
109 changes: 52 additions & 57 deletions cmd/mkunion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,54 +90,6 @@ func main() {
return nil
},
Commands: []*cli.Command{
{
Name: "match",
Description: "Generate custom pattern matching function",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Required: true,
},
},
Action: func(c *cli.Context) error {
cwd, _ := syscall.Getwd()
sourceName := path.Base(os.Getenv("GOFILE"))
sourcePath := path.Join(cwd, sourceName)

baseName := strings.TrimSuffix(sourceName, path.Ext(sourceName))

// file name without extension
inferred, err := generators.InferDeriveFuncMatchFromFile(sourcePath)
if err != nil {
return err
}

specName := c.String("name")
spec, err := inferred.MatchSpec(specName)
if err != nil {
return err
}

derived := generators.DeriveFuncMatchGenerator{
Header: "// Code generated by mkunion. DO NOT EDIT.",
PackageName: inferred.PackageName,
MatchSpec: *spec,
}

b, err := derived.Generate()
if err != nil {
return err
}
err = os.WriteFile(path.Join(
cwd,
baseName+"_match_"+strings.ToLower(derived.MatchSpec.Name)+".go"), b, 0644)
if err != nil {
return fmt.Errorf("failed to write %s for %s in %s: %w", "gen", derived.MatchSpec.Name, sourcePath, err)
}

return nil
},
},
{
Name: "shape-export",
Description: "Generate typescript types from golang types, and enable end-to-end type safety.",
Expand Down Expand Up @@ -265,23 +217,31 @@ func main() {
continue
}

// extract path name from event.Name
pathName := strings.Trim(event.Name, `"`)

if event.Op&fsnotify.Remove == fsnotify.Remove {
// if the file was removed, regenerate type registry
savedFiles, err := GenerateTypeRegistryForDir([]string{filepath.Dir(event.Name)})
dir := filepath.Dir(event.Name)

backLevel := log.GetLevel()
log.SetLevel(log.ErrorLevel)
savedFiles, err := GenerateTypeRegistryForDir([]string{dir})
log.SetLevel(backLevel)

if err != nil {
log.Warnf("failed to generate type registry for %s: %s", filepath.Dir(event.Name), err)
log.Warnf("failed to generate type registry for %s: %s", dir, err)
continue
}

for _, x := range savedFiles {
log.Infof("re-generated:\t%s", x)

justGenerated.Store(x, true)
}
}

// extract path name from event.Name
pathName := strings.Trim(event.Name, `"`)
// and, since file is deleted, skip the rest
continue
}

// if the file was generated by watch process, skip it
if _, ok := justGenerated.Load(pathName); ok {
Expand Down Expand Up @@ -532,7 +492,6 @@ func GenerateMain(sourcePaths []string, typeRegistry bool) ([]string, error) {
if err != nil {
return savedFiles, fmt.Errorf("failed generating union in %s: %w", sourcePath, err)
}

savedFile, err := SaveFile(contents, sourcePath, "union_gen")
if err != nil {
return savedFiles, fmt.Errorf("failed saving union in %s: %w", sourcePath, err)
Expand All @@ -545,7 +504,6 @@ func GenerateMain(sourcePaths []string, typeRegistry bool) ([]string, error) {
if err != nil {
return savedFiles, fmt.Errorf("failed generating serde in %s: %w", sourcePath, err)
}

savedFile, err = SaveFile(contents, sourcePath, "serde_gen")
if err != nil {
return savedFiles, fmt.Errorf("failed saving serde in %s: %w", sourcePath, err)
Expand All @@ -558,12 +516,22 @@ func GenerateMain(sourcePaths []string, typeRegistry bool) ([]string, error) {
if err != nil {
return savedFiles, fmt.Errorf("failed generating shape in %s: %w", sourcePath, err)
}

savedFile, err = SaveFile(contents, sourcePath, "shape_gen")
if err != nil {
return savedFiles, fmt.Errorf("failed saving shape in %s: %w", sourcePath, err)
}
if len(savedFile) > 0 {
savedFiles = append(savedFiles, savedFile)
}

contents, err = GenerateMatch(inferred)
if err != nil {
return savedFiles, fmt.Errorf("failed generating match in %s: %w", sourcePath, err)
}
savedFile, err = SaveFile(contents, sourcePath, "match_gen")
if err != nil {
return savedFiles, fmt.Errorf("failed saving match in %s: %w", sourcePath, err)
}
if len(savedFile) > 0 {
savedFiles = append(savedFiles, savedFile)
}
Expand Down Expand Up @@ -956,3 +924,30 @@ func GenerateTypeRegistry(inferred *shape.IndexedTypeWalker) (bytes.Buffer, erro

return contents, nil
}

func GenerateMatch(inferred *shape.InferredInfo) (bytes.Buffer, error) {
result := bytes.Buffer{}

match := generators.NewMkMatchTaggedNodeVisitor()
match.FromInferredInfo(inferred)

specs := match.Specs()
if len(specs) == 0 {
return result, nil
}

derived := generators.MkMatchGenerator{
Header: "// Code generated by mkunion. DO NOT EDIT.",
PackageName: inferred.PackageName(),
MatchSpecs: specs,
}

b, err := derived.Generate()

if err != nil {
return result, fmt.Errorf("GenerateMatch: failed to generate match: %w", err)
}

result.Write(b)
return result, nil
}

0 comments on commit c75ecdc

Please sign in to comment.