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

View File

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