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

Function enumValuesFromDump

scripts/check-scopes/main.go:98–137  ·  view source on GitHub ↗

enumValuesFromDump parses dump.sql and extracts all literals from the `CREATE TYPE api_key_scope AS ENUM (...)` block.

(path string)

Source from the content-addressed store, hash-verified

96// enumValuesFromDump parses dump.sql and extracts all literals from the
97// `CREATE TYPE api_key_scope AS ENUM (...)` block.
98func enumValuesFromDump(path string) (map[string]struct{}, error) {
99 f, err := os.Open(path)
100 if err != nil {
101 return nil, err
102 }
103 defer f.Close()
104
105 const enumHead = "CREATE TYPE api_key_scope AS ENUM ("
106 litRe := regexp.MustCompile(`'([^']+)'`)
107
108 values := make(map[string]struct{})
109 inEnum := false
110 s := bufio.NewScanner(f)
111 for s.Scan() {
112 line := strings.TrimSpace(s.Text())
113 if !inEnum {
114 if strings.Contains(line, enumHead) {
115 inEnum = true
116 }
117 continue
118 }
119 if strings.HasPrefix(line, ");") {
120 // End of enum block
121 return values, nil
122 }
123 // Collect single-quoted literals on this line.
124 for _, m := range litRe.FindAllStringSubmatch(line, -1) {
125 if len(m) > 1 {
126 values[m[1]] = struct{}{}
127 }
128 }
129 }
130 if err := s.Err(); err != nil {
131 return nil, err
132 }
133 if !inEnum {
134 return nil, xerrors.New("api_key_scope enum block not found in dump")
135 }
136 return values, nil
137}

Callers 1

mainFunction · 0.85

Calls 8

MustCompileMethod · 0.80
ErrMethod · 0.80
CloseMethod · 0.65
NewMethod · 0.65
OpenMethod · 0.45
ScanMethod · 0.45
TextMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected