MCPcopy
hub / github.com/dgraph-io/dgraph / parseScalarPair

Function parseScalarPair

schema/parse.go:84–166  ·  view source on GitHub ↗
(it *lex.ItemIterator, predicate string, ns uint64)

Source from the content-addressed store, hash-verified

82}
83
84func parseScalarPair(it *lex.ItemIterator, predicate string, ns uint64) (*pb.SchemaUpdate, error) {
85 it.Next()
86 next := it.Item()
87 switch {
88 // This check might seem redundant but it's necessary. We have two possibilities,
89 // 1) that the schema is of form: name@en: string .
90 //
91 // 2) or this alternate form: <name@en>: string .
92 //
93 // The itemAt test invalidates 1) and string.Contains() tests for 2). We don't allow
94 // '@' in predicate names, so both forms are disallowed. Handling them here avoids
95 // messing with the lexer and IRI values.
96 case next.Typ == itemAt || strings.Contains(predicate, "@"):
97 return nil, next.Errorf("Invalid '@' in name")
98 case next.Typ != itemColon:
99 return nil, next.Errorf("Missing colon")
100 case !it.Next():
101 return nil, next.Errorf("Invalid ending while trying to parse schema.")
102 }
103 next = it.Item()
104 schema := &pb.SchemaUpdate{Predicate: x.NamespaceAttr(ns, predicate)}
105 // Could be list type.
106 if next.Typ == itemLeftSquare {
107 schema.List = true
108 if !it.Next() {
109 return nil, next.Errorf("Invalid ending while trying to parse schema.")
110 }
111 next = it.Item()
112 }
113
114 if next.Typ != itemText {
115 return nil, next.Errorf("Missing Type")
116 }
117 typ := strings.ToLower(next.Val)
118 // We ignore the case for types.
119 t, ok := types.TypeForName(typ)
120 if !ok {
121 return nil, next.Errorf("Undefined Type")
122 }
123 if schema.List {
124 if uint32(t) == uint32(types.PasswordID) || uint32(t) == uint32(types.BoolID) {
125 return nil, next.Errorf("Unsupported type for list: [%s].", t.Name())
126 }
127 }
128 schema.ValueType = t.Enum()
129
130 // Check for index / reverse.
131 it.Next()
132 next = it.Item()
133 if schema.List {
134 if next.Typ != itemRightSquare {
135 return nil, next.Errorf("Unclosed [ while parsing schema for: %s", predicate)
136 }
137 if !it.Next() {
138 return nil, next.Errorf("Invalid ending")
139 }
140 next = it.Item()
141 }

Callers 1

parseFunction · 0.85

Calls 9

NamespaceAttrFunction · 0.92
TypeForNameFunction · 0.92
ItemMethod · 0.80
PrevMethod · 0.80
parseDirectiveFunction · 0.70
NameMethod · 0.65
NextMethod · 0.45
ErrorfMethod · 0.45
EnumMethod · 0.45

Tested by

no test coverage detected