MCPcopy
hub / github.com/openai/openai-go / literalStore

Method literalStore

internal/encoding/json/decode.go:866–1038  ·  view source on GitHub ↗

literalStore decodes a literal stored in item into v. fromQuoted indicates whether this literal came from unwrapping a string from the ",string" struct tag option. this is used only to produce more helpful error messages.

(item []byte, v reflect.Value, fromQuoted bool)

Source from the content-addressed store, hash-verified

864// string from the ",string" struct tag option. this is used only to
865// produce more helpful error messages.
866func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) error {
867 // Check for unmarshaler.
868 if len(item) == 0 {
869 // Empty string given.
870 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
871 return nil
872 }
873 isNull := item[0] == 'n' // null
874 u, ut, pv := indirect(v, isNull)
875 if u != nil {
876 return u.UnmarshalJSON(item)
877 }
878 if ut != nil {
879 if item[0] != '"' {
880 if fromQuoted {
881 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
882 return nil
883 }
884 val := "number"
885 switch item[0] {
886 case 'n':
887 val = "null"
888 case 't', 'f':
889 val = "bool"
890 }
891 d.saveError(&UnmarshalTypeError{Value: val, Type: v.Type(), Offset: int64(d.readIndex())})
892 return nil
893 }
894 s, ok := unquoteBytes(item)
895 if !ok {
896 if fromQuoted {
897 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())
898 }
899 panic(phasePanicMsg)
900 }
901 return ut.UnmarshalText(s)
902 }
903
904 v = pv
905
906 switch c := item[0]; c {
907 case 'n': // null
908 // The main parser checks that only true and false can reach here,
909 // but if this was a quoted string input, it could be anything.
910 if fromQuoted && string(item) != "null" {
911 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
912 break
913 }
914 switch v.Kind() {
915 case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice:
916 v.SetZero()
917 // otherwise, ignore null for primitives/string
918 }
919 case 't', 'f': // true, false
920 value := item[0] == 't'
921 // The main parser checks that only true and false can reach here,
922 // but if this was a quoted string input, it could be anything.
923 if fromQuoted && string(item) != "true" && string(item) != "false" {

Callers 2

valueMethod · 0.95
objectMethod · 0.95

Calls 10

saveErrorMethod · 0.95
readIndexMethod · 0.95
convertNumberMethod · 0.95
indirectFunction · 0.85
unquoteBytesFunction · 0.85
isValidNumberFunction · 0.85
DecodeMethod · 0.80
OverflowIntMethod · 0.80
OverflowUintMethod · 0.80
UnmarshalJSONMethod · 0.65

Tested by

no test coverage detected