Skip to content
This repository was archived by the owner on Jan 18, 2026. It is now read-only.

Commit d0e80f0

Browse files
authored
feat: add ParseBytes variants for parsing schemas (#268)
1 parent b3aa1f7 commit d0e80f0

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

schema_parse.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,12 @@ var DefaultSchemaCache = &SchemaCache{}
1717

1818
// Parse parses a schema string.
1919
func Parse(schema string) (Schema, error) {
20-
return ParseWithCache(schema, "", DefaultSchemaCache)
20+
return ParseBytes([]byte(schema))
2121
}
2222

23-
// ParseWithCache parses a schema string using the given namespace and schema cache.
23+
// ParseWithCache parses a schema string using the given namespace and schema cache.
2424
func ParseWithCache(schema, namespace string, cache *SchemaCache) (Schema, error) {
25-
var json any
26-
if err := jsoniter.Unmarshal([]byte(schema), &json); err != nil {
27-
json = schema
28-
}
29-
30-
return parseType(namespace, json, cache)
25+
return ParseBytesWithCache([]byte(schema), namespace, cache)
3126
}
3227

3328
// MustParse parses a schema string, panicing if there is an error.
@@ -60,6 +55,21 @@ func ParseFiles(paths ...string) (Schema, error) {
6055
return schema, nil
6156
}
6257

58+
// ParseBytes parses a schema byte slice.
59+
func ParseBytes(schema []byte) (Schema, error) {
60+
return ParseBytesWithCache(schema, "", DefaultSchemaCache)
61+
}
62+
63+
// ParseBytesWithCache parses a schema byte slice using the given namespace and schema cache.
64+
func ParseBytesWithCache(schema []byte, namespace string, cache *SchemaCache) (Schema, error) {
65+
var json any
66+
if err := jsoniter.Unmarshal(schema, &json); err != nil {
67+
json = string(schema)
68+
}
69+
70+
return parseType(namespace, json, cache)
71+
}
72+
6373
func parseType(namespace string, v any, cache *SchemaCache) (Schema, error) {
6474
switch val := v.(type) {
6575
case nil:

0 commit comments

Comments
 (0)