diff --git a/example/main.go b/example/main.go index ccfe8b5..9752a98 100644 --- a/example/main.go +++ b/example/main.go @@ -107,11 +107,27 @@ func main() { log.Panic(err) } + currentSchemas, err := client.GetSchemas() + if err != nil { + log.Panic(err) + } + + currentSchemas.ForEachSchema(func(schemaName string) { + + schemaResp, err := client.GetSchema(schemaName) + if err != nil { + log.Panic(err) + } + + fmt.Println("Reading Schema:") + fmt.Println(schemaResp) + + }) } func getSchema() pinotModel.Schema { - schemaFilePath := "./data-gen/block_header_schema.json" + schemaFilePath := "./example/data-gen/block_header_schema.json" f, err := os.Open(schemaFilePath) if err != nil { diff --git a/go-pinot-api.go b/go-pinot-api.go index 60829e2..542cb63 100644 --- a/go-pinot-api.go +++ b/go-pinot-api.go @@ -72,8 +72,6 @@ type ValidateSchemaResponse struct { Error string } -type GetSchemaResponse []string - func (c *PinotAPIClient) FetchData(endpoint string, result any) error { fullURL := fullUrl(c.pinotControllerUrl, endpoint) @@ -314,12 +312,20 @@ func (c *PinotAPIClient) GetTenants() (*GetTenantsResponse, error) { } // GetSchemas returns a list of schemas -func (c *PinotAPIClient) GetSchemas() (*GetSchemaResponse, error) { - var result GetSchemaResponse +func (c *PinotAPIClient) GetSchemas() (*model.GetSchemaResponse, error) { + var result model.GetSchemaResponse err := c.FetchData("/schemas", &result) return &result, err } +// GetSchema returns a schema +func (c *PinotAPIClient) GetSchema(schemaName string) (*model.Schema, error) { + var result model.Schema + err := c.FetchData(fmt.Sprintf("/schemas/%s", schemaName), &result) + return &result, err + +} + // CreateSchema creates a new schema. // if it already exists, it will nothing will happen func (c *PinotAPIClient) CreateSchema(schema model.Schema) (*model.UserActionResponse, error) { diff --git a/model/GetSchemaResponse.go b/model/GetSchemaResponse.go new file mode 100644 index 0000000..fa58a09 --- /dev/null +++ b/model/GetSchemaResponse.go @@ -0,0 +1,9 @@ +package model + +type GetSchemaResponse []string + +func (g *GetSchemaResponse) ForEachSchema(apply func(schemaName string)) { + for _, v := range *g { + apply(v) + } +} diff --git a/model/Schema.go b/model/Schema.go index af13e17..853bae9 100644 --- a/model/Schema.go +++ b/model/Schema.go @@ -12,9 +12,9 @@ type FieldSpec struct { type Schema struct { SchemaName string `json:"schemaName"` DimensionFieldSpecs []FieldSpec `json:"dimensionFieldSpecs"` - MetricFieldSpecs []FieldSpec `json:"metricFieldSpecs"` - DateTimeFieldSpecs []FieldSpec `json:"dateTimeFieldSpecs"` - PrimaryKeyColumns []string `json:"primaryKeyColumns"` + MetricFieldSpecs []FieldSpec `json:"metricFieldSpecs,omitempty"` + DateTimeFieldSpecs []FieldSpec `json:"dateTimeFieldSpecs,omitempty"` + PrimaryKeyColumns []string `json:"primaryKeyColumns,omitempty"` } func (schema *Schema) AsBytes() ([]byte, error) { @@ -27,3 +27,11 @@ func (schema *Schema) AsBytes() ([]byte, error) { return schemaBytes, nil } + +func (schema *Schema) String() string { + jsonString, err := json.MarshalIndent(schema, "", " ") + if err != nil { + return err.Error() + } + return string(jsonString) +}