mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
Move the command to cmd/opencode2api and group production code into configuration, gateway, protocol, model, admin, telemetry and helper packages. Keep shared state private behind context, metrics and runtime interfaces. Remove the requested test files and replace test-only CI jobs with vet/build checks. Update Docker and release build paths, preserve version injection and make the WebUI an explicit embedded asset package. Add directory trees and source reading guides to both READMEs. Anchor binary ignore rules so the command package is tracked.
75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
)
|
|
|
|
// stripJSONComments removes // and /* */ comments without changing newlines,
|
|
// so syntax errors still point at the correct line in config.json. Comment
|
|
// markers inside JSON strings (for example, https:// URLs) are preserved.
|
|
func stripJSONComments(data []byte) ([]byte, error) {
|
|
data = bytes.TrimPrefix(data, []byte{0xef, 0xbb, 0xbf})
|
|
out := make([]byte, 0, len(data))
|
|
inString := false
|
|
escaped := false
|
|
lineComment := false
|
|
blockComment := false
|
|
|
|
for i := 0; i < len(data); i++ {
|
|
current := data[i]
|
|
if lineComment {
|
|
if current == '\n' || current == '\r' {
|
|
lineComment = false
|
|
out = append(out, current)
|
|
} else {
|
|
out = append(out, ' ')
|
|
}
|
|
continue
|
|
}
|
|
if blockComment {
|
|
if current == '*' && i+1 < len(data) && data[i+1] == '/' {
|
|
out = append(out, ' ', ' ')
|
|
i++
|
|
blockComment = false
|
|
} else if current == '\n' || current == '\r' {
|
|
out = append(out, current)
|
|
} else {
|
|
out = append(out, ' ')
|
|
}
|
|
continue
|
|
}
|
|
if inString {
|
|
out = append(out, current)
|
|
if escaped {
|
|
escaped = false
|
|
} else if current == '\\' {
|
|
escaped = true
|
|
} else if current == '"' {
|
|
inString = false
|
|
}
|
|
continue
|
|
}
|
|
|
|
switch {
|
|
case current == '"':
|
|
inString = true
|
|
out = append(out, current)
|
|
case current == '/' && i+1 < len(data) && data[i+1] == '/':
|
|
lineComment = true
|
|
out = append(out, ' ', ' ')
|
|
i++
|
|
case current == '/' && i+1 < len(data) && data[i+1] == '*':
|
|
blockComment = true
|
|
out = append(out, ' ', ' ')
|
|
i++
|
|
default:
|
|
out = append(out, current)
|
|
}
|
|
}
|
|
if blockComment {
|
|
return nil, errors.New("unterminated block comment")
|
|
}
|
|
return out, nil
|
|
}
|