1
0
Viktor Sokolov 1 сар өмнө
parent
commit
5954d0d1b3

+ 7 - 3
imagetype_new/registry.go

@@ -40,14 +40,18 @@ func GetType(t Type) *TypeDesc {
 // It panics if the type already exists (i.e., if a TypeDesc is already registered for this Type).
 func (r *Registry) RegisterType(desc *TypeDesc) Type {
 	r.types = append(r.types, desc)
-	return Type(len(r.types) - 1) // -1 is unknown
+	return Type(len(r.types)) // 0 is unknown
 }
 
 // GetType returns the TypeDesc for the given Type.
 // Returns nil if the type is not registered.
 func (r *Registry) GetType(t Type) *TypeDesc {
-	if int(t) >= len(r.types) {
+	if t == Unknown {
 		return nil
 	}
-	return r.types[t]
+
+	if int(t-1) >= len(r.types) {
+		return nil
+	}
+	return r.types[t-1]
 }

+ 3 - 1
imagetype_new/type.go

@@ -12,7 +12,9 @@ type (
 
 // Supported image types
 var (
-	Unknown Type = -1
+	// Unknown is a reserved type, it has index 0. We guarantee that index 0 won't be used
+	// for any other type. This way, Unknown is a zero value for Type.
+	Unknown Type = 0
 
 	JPEG = RegisterType(&TypeDesc{
 		String:                "jpeg",