checkpoint on new objectservice

This commit is contained in:
sawka
2024-05-26 23:05:11 -07:00
parent b1aaba2a37
commit 95ce1cc86d
9 changed files with 387 additions and 47 deletions
+23 -16
View File
@@ -23,6 +23,11 @@ const (
VersionGoFieldName = "Version"
)
type ORef struct {
OType string `json:"otype"`
OID string `json:"oid"`
}
type WaveObj interface {
GetOType() string // should not depend on object state (should work with nil value)
}
@@ -36,35 +41,37 @@ type waveObjDesc struct {
var waveObjMap = sync.Map{}
var waveObjRType = reflect.TypeOf((*WaveObj)(nil)).Elem()
func RegisterType[T WaveObj]() {
var waveObj T
func RegisterType(rtype reflect.Type) {
if rtype.Kind() != reflect.Ptr {
panic(fmt.Sprintf("wave object must be a pointer for %v", rtype))
}
if !rtype.Implements(waveObjRType) {
panic(fmt.Sprintf("wave object must implement WaveObj for %v", rtype))
}
waveObj := reflect.Zero(rtype).Interface().(WaveObj)
otype := waveObj.GetOType()
if otype == "" {
panic(fmt.Sprintf("otype is empty for %T", waveObj))
}
rtype := reflect.TypeOf(waveObj)
if rtype.Kind() != reflect.Ptr {
panic(fmt.Sprintf("wave object must be a pointer for %T", waveObj))
panic(fmt.Sprintf("otype is empty for %v", rtype))
}
oidField, found := rtype.Elem().FieldByName(OIDGoFieldName)
if !found {
panic(fmt.Sprintf("missing OID field for %T", waveObj))
panic(fmt.Sprintf("missing OID field for %v", rtype))
}
if oidField.Type.Kind() != reflect.String {
panic(fmt.Sprintf("OID field must be string for %T", waveObj))
panic(fmt.Sprintf("OID field must be string for %v", rtype))
}
if oidField.Tag.Get("json") != OIDKeyName {
panic(fmt.Sprintf("OID field json tag must be %q for %T", OIDKeyName, waveObj))
panic(fmt.Sprintf("OID field json tag must be %q for %v", OIDKeyName, rtype))
}
versionField, found := rtype.Elem().FieldByName(VersionGoFieldName)
if !found {
panic(fmt.Sprintf("missing Version field for %T", waveObj))
panic(fmt.Sprintf("missing Version field for %v", rtype))
}
if versionField.Type.Kind() != reflect.Int {
panic(fmt.Sprintf("Version field must be int for %T", waveObj))
panic(fmt.Sprintf("Version field must be int for %v", rtype))
}
if versionField.Tag.Get("json") != VersionKeyName {
panic(fmt.Sprintf("Version field json tag must be %q for %T", VersionKeyName, waveObj))
panic(fmt.Sprintf("Version field json tag must be %q for %v", VersionKeyName, rtype))
}
_, found = waveObjMap.Load(otype)
if found {
@@ -286,16 +293,16 @@ func generateTSTypeInternal(rtype reflect.Type) (string, []reflect.Type) {
subTypes = append(subTypes, fieldSubTypes...)
buf.WriteString(fmt.Sprintf(" %s%s: %s;\n", fieldName, optMarker, tsType))
}
buf.WriteString("}\n")
buf.WriteString("};\n")
return buf.String(), subTypes
}
func GenerateWaveObjTSType() string {
var buf bytes.Buffer
buf.WriteString("type WaveObj {\n")
buf.WriteString("type WaveObj = {\n")
buf.WriteString(" otype: string;\n")
buf.WriteString(" oid: string;\n")
buf.WriteString("}\n")
buf.WriteString("};\n")
return buf.String()
}