stash/vendor/github.com/vektah/gqlparser/v2/ast/path.go
WithoutPants 7a45943e8e
Stash box client interface (#751)
* Add gql client generation files
* Update dependencies
* Add stash-box client generation to the makefile
* Move scraped scene object matchers to models
* Add stash-box to scrape with dropdown
* Add scrape scene from fingerprint in UI
2020-09-17 19:57:18 +10:00

67 lines
1.2 KiB
Go

package ast
import (
"bytes"
"encoding/json"
"fmt"
)
var _ json.Unmarshaler = (*Path)(nil)
type Path []PathElement
type PathElement interface {
isPathElement()
}
var _ PathElement = PathIndex(0)
var _ PathElement = PathName("")
func (path Path) String() string {
var str bytes.Buffer
for i, v := range path {
switch v := v.(type) {
case PathIndex:
str.WriteString(fmt.Sprintf("[%d]", v))
case PathName:
if i != 0 {
str.WriteByte('.')
}
str.WriteString(string(v))
default:
panic(fmt.Sprintf("unknown type: %T", v))
}
}
return str.String()
}
func (path *Path) UnmarshalJSON(b []byte) error {
var vs []interface{}
err := json.Unmarshal(b, &vs)
if err != nil {
return err
}
*path = make([]PathElement, 0, len(vs))
for _, v := range vs {
switch v := v.(type) {
case string:
*path = append(*path, PathName(v))
case int:
*path = append(*path, PathIndex(v))
case float64:
*path = append(*path, PathIndex(int(v)))
default:
return fmt.Errorf("unknown path element type: %T", v)
}
}
return nil
}
type PathIndex int
func (_ PathIndex) isPathElement() {}
type PathName string
func (_ PathName) isPathElement() {}