MCPcopy Index your code
hub / github.com/coder/coder / generateConstraints

Function generateConstraints

scripts/dbgen/constraint.go:84–141  ·  view source on GitHub ↗

generateConstraints does the following: 1. Read the dump.sql file 2. Parse the file into each query 3. Pass each query to the constraintFn function 4. Generate the enum from the returned constraints 5. Write the generated code to the output path

(dumpPath, outputPath string, outputConstraintType constraintType, fn queryToConstraintsFn)

Source from the content-addressed store, hash-verified

82// 4. Generate the enum from the returned constraints
83// 5. Write the generated code to the output path
84func generateConstraints(dumpPath, outputPath string, outputConstraintType constraintType, fn queryToConstraintsFn) error {
85 dump, err := os.Open(dumpPath)
86 if err != nil {
87 return err
88 }
89 defer dump.Close()
90
91 var allConstraints []constraint
92
93 dumpScanner := bufio.NewScanner(dump)
94 query := ""
95 for dumpScanner.Scan() {
96 line := strings.TrimSpace(dumpScanner.Text())
97 switch {
98 case strings.HasPrefix(line, "--"):
99 case line == "":
100 case strings.HasSuffix(line, ";"):
101 query += line
102 newConstraints, err := fn(query)
103 query = ""
104 if err != nil {
105 return xerrors.Errorf("process query %q: %w", query, err)
106 }
107 allConstraints = append(allConstraints, newConstraints...)
108 default:
109 query += line + " "
110 }
111 }
112 if err = dumpScanner.Err(); err != nil {
113 return err
114 }
115
116 s := &bytes.Buffer{}
117
118 _, _ = fmt.Fprintf(s, `// Code generated by scripts/dbgen/main.go. DO NOT EDIT.
119package database
120
121// %[1]s represents a named %[2]s constraint on a table.
122type %[1]s string
123
124// %[1]s enums.
125const (
126`, outputConstraintType.goType(), outputConstraintType.goTypeDescriptionPart())
127
128 for _, c := range allConstraints {
129 constName := outputConstraintType.goEnumNamePrefix() + nameFromSnakeCase(c.name)
130 _, _ = fmt.Fprintf(s, "\t%[1]s %[2]s = %[3]q // %[4]s\n", constName, outputConstraintType.goType(), c.name, c.comment)
131 }
132 _, _ = fmt.Fprint(s, ")\n")
133
134 data, err := imports.Process(outputPath, s.Bytes(), &imports.Options{
135 Comments: true,
136 })
137 if err != nil {
138 return err
139 }
140 return atomicwrite.File(outputPath, data)
141}

Callers 3

generateCheckConstraintsFunction · 0.85

Calls 12

FileFunction · 0.92
nameFromSnakeCaseFunction · 0.85
ErrMethod · 0.80
goTypeMethod · 0.80
goTypeDescriptionPartMethod · 0.80
goEnumNamePrefixMethod · 0.80
CloseMethod · 0.65
OpenMethod · 0.45
ScanMethod · 0.45
TextMethod · 0.45
ErrorfMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected