MCPcopy
hub / github.com/wavetermdev/waveterm / RegisterType

Function RegisterType

pkg/waveobj/waveobj.go:123–174  ·  view source on GitHub ↗
(rtype reflect.Type)

Source from the content-addressed store, hash-verified

121var metaMapRType = reflect.TypeOf(MetaMapType{})
122
123func RegisterType(rtype reflect.Type) {
124 if rtype.Kind() != reflect.Ptr {
125 panic(fmt.Sprintf("wave object must be a pointer for %v", rtype))
126 }
127 if !rtype.Implements(waveObjRType) {
128 panic(fmt.Sprintf("wave object must implement WaveObj for %v", rtype))
129 }
130 waveObj := reflect.Zero(rtype).Interface().(WaveObj)
131 otype := waveObj.GetOType()
132 if otype == "" {
133 panic(fmt.Sprintf("otype is empty for %v", rtype))
134 }
135 oidField, found := rtype.Elem().FieldByName(OIDGoFieldName)
136 if !found {
137 panic(fmt.Sprintf("missing OID field for %v", rtype))
138 }
139 if oidField.Type.Kind() != reflect.String {
140 panic(fmt.Sprintf("OID field must be string for %v", rtype))
141 }
142 oidJsonTag := utilfn.GetJsonTag(oidField)
143 if oidJsonTag != OIDKeyName {
144 panic(fmt.Sprintf("OID field json tag must be %q for %v", OIDKeyName, rtype))
145 }
146 versionField, found := rtype.Elem().FieldByName(VersionGoFieldName)
147 if !found {
148 panic(fmt.Sprintf("missing Version field for %v", rtype))
149 }
150 if versionField.Type.Kind() != reflect.Int {
151 panic(fmt.Sprintf("Version field must be int for %v", rtype))
152 }
153 versionJsonTag := utilfn.GetJsonTag(versionField)
154 if versionJsonTag != VersionKeyName {
155 panic(fmt.Sprintf("Version field json tag must be %q for %v", VersionKeyName, rtype))
156 }
157 metaField, found := rtype.Elem().FieldByName(MetaGoFieldName)
158 if !found {
159 panic(fmt.Sprintf("missing Meta field for %v", rtype))
160 }
161 if metaField.Type != metaMapRType {
162 panic(fmt.Sprintf("Meta field must be MetaMapType for %v", rtype))
163 }
164 _, found = waveObjMap.Load(otype)
165 if found {
166 panic(fmt.Sprintf("otype %q already registered", otype))
167 }
168 waveObjMap.Store(otype, &waveObjDesc{
169 RType: rtype,
170 OIDField: oidField,
171 VersionField: versionField,
172 MetaField: metaField,
173 })
174}
175
176func getWaveObjDesc(otype string) *waveObjDesc {
177 desc, _ := waveObjMap.Load(otype)

Callers 1

initFunction · 0.92

Calls 2

GetJsonTagFunction · 0.92
GetOTypeMethod · 0.65

Tested by

no test coverage detected