Documentation ¶
Index ¶
- Constants
- type Field
- type Fields
- type ForeignKeyReference
- type ForeignKeys
- type GeoPoint
- type Schema
- func (s *Schema) Decode(row []string, out interface{}) error
- func (s *Schema) DecodeTable(tab table.Table, out interface{}) error
- func (s *Schema) Encode(in interface{}) ([]string, error)
- func (s *Schema) EncodeTable(in interface{}) ([][]string, error)
- func (s *Schema) GetField(name string) (*Field, int)
- func (s *Schema) HasField(name string) bool
- func (s *Schema) Headers() []string
- func (s *Schema) MarshalJSON() ([]byte, error)
- func (s *Schema) SaveToFile(path string) error
- func (s *Schema) UnmarshalJSON(data []byte) error
- func (s *Schema) Validate() error
- func (s *Schema) Write(w io.Writer) error
Examples ¶
Constants ¶
const ( IntegerType = "integer" StringType = "string" BooleanType = "boolean" NumberType = "number" DateType = "date" ObjectType = "object" ArrayType = "array" DateTimeType = "datetime" TimeType = "time" YearMonthType = "yearmonth" YearType = "year" DurationType = "duration" GeoPointType = "geopoint" )
Field types.
const ( GeoPointArrayFormat = "array" GeoPointObjectFormat = "object" )
Formats specific to GeoPoint field type.
const (
AnyDateFormat = "any"
)
Formats.
const InvalidPosition = -1
InvalidPosition is returned by GetField call when it refers to a field that does not exist in the schema.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Field ¶
type Field struct { // Name of the field. It is mandatory and shuold correspond to the name of field/column in the data file (if it has a name). Name string `json:"name"` Type string `json:"type,omitempty"` Format string `json:"format,omitempty"` // A human readable label or title for the field. Title string `json:"title,omitempty"` // A description for this field e.g. "The recipient of the funds" Description string `json:"description,omitempty"` // Boolean properties. Define set of the values that represent true and false, respectively. // https://specs.frictionlessdata.io/table-schema/#boolean TrueValues []string `json:"trueValues,omitempty"` FalseValues []string `json:"falseValues,omitempty"` }
Field describes a single field in the table schema. More: https://specs.frictionlessdata.io/table-schema/#field-descriptors
func (*Field) Decode ¶ added in v0.1.2
Decode decodes the passed-in string against field type. Returns an error if the value can not be cast or any field constraint can not be satisfied.
func (*Field) Encode ¶ added in v0.1.2
Encode encodes the passed-in value into a string. It returns an error if the the type of the passed-in value can not be converted to field type.
func (*Field) TestString ¶ added in v0.1.2
TestString checks whether the value can be unmarshalled to the field type.
func (*Field) UnmarshalJSON ¶
UnmarshalJSON sets *f to a copy of data. It will respect the default values described at: https://specs.frictionlessdata.io/table-schema/
type ForeignKeyReference ¶
type ForeignKeyReference struct { Resource string `json:"resource,omitempty"` Fields []string `json:"-"` FieldsPlaceholder interface{} `json:"fields,omitempty"` }
ForeignKeyReference represents the field reference by a foreign key.
type ForeignKeys ¶
type ForeignKeys struct { Fields []string `json:"-"` FieldsPlaceholder interface{} `json:"fields,omitempty"` Reference ForeignKeyReference `json:"reference,omitempty"` }
ForeignKeys defines a schema foreign key
type GeoPoint ¶
GeoPoint represents a "geopoint" cell. More at: https://specs.frictionlessdata.io/table-schema/#geopoint
func (*GeoPoint) UnmarshalJSON ¶
UnmarshalJSON sets *f to a copy of data. It will respect the default values
type Schema ¶
type Schema struct { Fields Fields `json:"fields,omitempty"` PrimaryKeyPlaceholder interface{} `json:"primaryKey,omitempty"` PrimaryKeys []string `json:"-"` ForeignKeys ForeignKeys `json:"foreignKeys,omitempty"` MissingValues []string `json:"missingValues,omitempty"` }
Schema describes tabular data.
func Infer ¶
Infer infers a schema from a slice of the tabular data. For columns that contain cells that can inferred as different types, the most popular type is set as the field type. For instance, a column with values 10.1, 10, 10 will inferred as being of type "integer".
func InferImplicitCasting ¶
InferImplicitCasting uses a implicit casting for infering the type of columns that have cells of diference types. For instance, a column with values 10.1, 10, 10 will inferred as being of type "number" ("integer" can be implicitly cast to "number").
For medium to big tables, this method is faster than the Infer.
Example ¶
tab := table.FromSlices( []string{"Person", "Height"}, [][]string{ []string{"Foo", "5"}, []string{"Bar", "4"}, []string{"Bez", "5.5"}, }) s, _ := InferImplicitCasting(tab) fmt.Println("Fields:") for _, f := range s.Fields { fmt.Printf("{Name:%s Type:%s Format:%s}\n", f.Name, f.Type, f.Format) }
Output: Fields: {Name:Person Type:string Format:default} {Name:Height Type:number Format:default}
func Read ¶
Read reads and parses a descriptor to create a schema.
Example - Reading a schema from a file:
f, err := os.Open("foo/bar/schema.json") if err != nil { panic(err) } s, err := Read(f) if err != nil { panic(err) } fmt.Println(s)
func ReadFromFile ¶
ReadFromFile reads and parses a schema descrptor from a local file.
func (*Schema) Decode ¶ added in v0.1.2
Decode decodes the passed-in row to schema types and stores it in the value pointed by out. The out value must be pointer to a struct. Only exported fields will be unmarshalled. The lowercased field name is used as the key for each exported field.
If a value in the row cannot be marshalled to its respective schema field (Field.Unmarshal), this call will return an error. Furthermore, this call is also going to return an error if the schema field value can not be unmarshalled to the struct field type.
Example ¶
// Lets assume we have a schema ... s := Schema{Fields: []Field{{Name: "Name", Type: StringType}, {Name: "Age", Type: IntegerType}}} // And a Table. t := table.FromSlices([]string{"Name", "Age"}, [][]string{ {"Foo", "42"}, {"Bar", "43"}}) // And we would like to process them using Go types. First we need to create a struct to hold the // content of each row. type person struct { Name string Age int } // Now it is a matter of iterate over the table and Decode each row. iter, _ := t.Iter() for iter.Next() { var p person s.Decode(iter.Row(), &p) fmt.Printf("%+v\n", p) }
Output: {Name:Foo Age:42} {Name:Bar Age:43}
func (*Schema) DecodeTable ¶ added in v0.1.2
DecodeTable loads and decodes all table rows.
The result argument must necessarily be the address for a slice. The slice may be nil or previously allocated.
Example ¶
// Lets assume we have a schema ... s := Schema{Fields: []Field{{Name: "Name", Type: StringType}, {Name: "Age", Type: IntegerType}}} // And a Table. t := table.FromSlices([]string{"Name", "Age"}, [][]string{ {"Foo", "42"}, {"Bar", "43"}}) // And we would like to process them using Go types. First we need to create a struct to hold the // content of each row. type person struct { Name string Age int } var people []person s.DecodeTable(t, &people) fmt.Print(people)
Output: [{Foo 42} {Bar 43}]
func (*Schema) Encode ¶ added in v0.1.2
Encode encodes struct into a row. This method can only encode structs (or pointer to structs) and will error out if nil is passed.
Example ¶
// Lets assume we have a schema. s := Schema{Fields: []Field{{Name: "Name", Type: StringType}, {Name: "Age", Type: IntegerType}}} // And would like to create a CSV out of this list conforming to // to the schema above. people := []struct { Name string Age int }{{"Foo", 42}, {"Bar", 43}} // First create the writer and write the header. w := table.NewStringWriter() w.Write([]string{"Name", "Age"}) // Then write the list for _, person := range people { row, _ := s.Encode(person) w.Write(row) } w.Flush() fmt.Print(w.String())
Output: Name,Age Foo,42 Bar,43
func (*Schema) EncodeTable ¶ added in v0.1.2
EncodeTable encodes each element (struct) of the passed-in slice and
Example ¶
// Lets assume we have a schema. s := Schema{Fields: []Field{{Name: "Name", Type: StringType}, {Name: "Age", Type: IntegerType}}} // And would like to create a CSV out of this list conforming to // to the schema above. people := []struct { Name string Age int }{{"Foo", 42}, {"Bar", 43}} // Then encode the people slice into a slice of rows. rows, _ := s.EncodeTable(people) // Now, simply write it down. w := table.NewStringWriter() w.Write([]string{"Name", "Age"}) w.WriteAll(rows) w.Flush() fmt.Print(w.String())
Output: Name,Age Foo,42 Bar,43
func (*Schema) HasField ¶
HasField returns checks whether the schema has a field with the passed-in.
func (*Schema) MarshalJSON ¶
MarshalJSON returns the JSON encoding of s.
func (*Schema) SaveToFile ¶
SaveToFile writes the schema descriptor in local file.
func (*Schema) UnmarshalJSON ¶
UnmarshalJSON sets *f to a copy of data. It will respect the default values described at: https://specs.frictionlessdata.io/table-schema/
func (*Schema) Validate ¶
Validate checks whether the schema is valid. If it is not, returns an error describing the problem. More at: https://specs.frictionlessdata.io/table-schema/