x/model: disambiguate type names

There are now more than just File to the model package.  This change
disambiguates types like Error to FileError, etc. It also helps line
things up with GoDoc.
This commit is contained in:
Blake Mizerany 2024-04-04 14:09:26 -07:00
parent 58de2b8d4a
commit 2633fcb149
2 changed files with 20 additions and 18 deletions

View File

@ -1,3 +1,4 @@
// Package model implements the Modelfile and Path formats.
package model
import (
@ -7,12 +8,12 @@ import (
"strings"
)
type Param struct {
type ParameterPragma struct {
Key string
Value string
}
type Message struct {
type MessagePragma struct {
Role string
Content string
}
@ -23,24 +24,25 @@ type File struct {
From string
// Optional
Params []Param
Params []ParameterPragma
Template string
System string
Adapter string
Messages []Message
Messages []MessagePragma
License string
}
type Error struct {
type FileError struct {
Pragma string
Message string
}
func (e *Error) Error() string {
func (e *FileError) Error() string {
return e.Pragma + ": " + e.Message
}
// Pragma represents a single pragma in a Modelfile.
type Pragma struct {
// The pragma name
Name string
@ -57,7 +59,7 @@ func (p Pragma) Arg(i int) string {
return p.Args[i]
}
func Pragmas(r io.Reader) iter.Seq2[Pragma, error] {
func FilePragmas(r io.Reader) iter.Seq2[Pragma, error] {
return func(yield func(Pragma, error) bool) {
sc := bufio.NewScanner(r)
for sc.Scan() {
@ -93,9 +95,9 @@ func Pragmas(r io.Reader) iter.Seq2[Pragma, error] {
}
}
func Decode(r io.Reader) (File, error) {
func ParseFile(r io.Reader) (File, error) {
var f File
for p, err := range Pragmas(r) {
for p, err := range FilePragmas(r) {
if err != nil {
return File{}, err
}
@ -103,7 +105,7 @@ func Decode(r io.Reader) (File, error) {
case "FROM":
f.From = p.Arg(0)
case "PARAMETER":
f.Params = append(f.Params, Param{
f.Params = append(f.Params, ParameterPragma{
Key: strings.ToLower(p.Arg(0)),
Value: p.Arg(1),
})
@ -114,7 +116,7 @@ func Decode(r io.Reader) (File, error) {
case "ADAPTER":
f.Adapter = p.Arg(0)
case "MESSAGE":
f.Messages = append(f.Messages, Message{
f.Messages = append(f.Messages, MessagePragma{
Role: p.Arg(0),
Content: p.Arg(1),
})

View File

@ -9,11 +9,11 @@ import (
const MaxPathLength = 255
type PartKind int
type PathPart int
// Levels of concreteness
const (
Invalid PartKind = iota
Invalid PathPart = iota
Domain
Namespace
Name
@ -21,7 +21,7 @@ const (
Build
)
var kindNames = map[PartKind]string{
var kindNames = map[PathPart]string{
Invalid: "Invalid",
Domain: "Domain",
Namespace: "Namespace",
@ -153,7 +153,7 @@ func (r Path) Build() string { return r.build }
// ""
func ParsePath(s string) Path {
var r Path
for kind, part := range Parts(s) {
for kind, part := range PathParts(s) {
switch kind {
case Domain:
r.domain = part
@ -196,8 +196,8 @@ func Merge(a, b Path) Path {
//
// It normalizes the input string by removing "http://" and "https://" only.
// No other normalization is done.
func Parts(s string) iter.Seq2[PartKind, string] {
return func(yield func(PartKind, string) bool) {
func PathParts(s string) iter.Seq2[PathPart, string] {
return func(yield func(PathPart, string) bool) {
if strings.HasPrefix(s, "http://") {
s = s[len("http://"):]
}
@ -209,7 +209,7 @@ func Parts(s string) iter.Seq2[PartKind, string] {
return
}
yieldValid := func(kind PartKind, part string) bool {
yieldValid := func(kind PathPart, part string) bool {
if !isValidPart(part) {
yield(Invalid, "")
return false