activity/tools/defs/types.go

133 行
2.8 KiB
Go
Raw 通常表示 履歴

2018-01-24 08:00:59 +09:00
package defs
type TypeMetadata struct {
HasIsPublicMethod bool
}
2018-01-24 08:00:59 +09:00
type Type struct {
Name string
URI string
Notes string
DisjointWith []*Type
Extends []*Type
Properties []*PropertyType
WithoutProperties []*PropertyType
Meta TypeMetadata
}
func (t *Type) GetTypeMetadata() TypeMetadata {
m := t.Meta
for _, parent := range t.Extends {
parentMeta := parent.GetTypeMetadata()
if parentMeta.HasIsPublicMethod {
m.HasIsPublicMethod = true
}
}
return m
2018-01-24 08:00:59 +09:00
}
func (t *Type) GetProperties() []*PropertyType {
2018-01-26 08:19:21 +09:00
omit := make(map[*PropertyType]bool)
2018-01-24 08:00:59 +09:00
for _, v := range t.WithoutProperties {
2018-01-26 08:19:21 +09:00
omit[v] = true
2018-01-24 08:00:59 +09:00
}
var parentProps []*PropertyType
for _, p := range t.Extends {
parentProps = append(parentProps, p.GetProperties()...)
}
properties := make([]*PropertyType, 0, len(t.Properties)+len(parentProps))
set := make(map[string]bool)
for _, v := range t.Properties {
2018-01-26 08:19:21 +09:00
if !omit[v] {
2018-01-24 08:00:59 +09:00
if !set[v.Name] {
properties = append(properties, v)
set[v.Name] = true
}
}
}
for _, v := range parentProps {
2018-01-26 08:19:21 +09:00
if !omit[v] {
2018-01-24 08:00:59 +09:00
if !set[v.Name] {
properties = append(properties, v)
set[v.Name] = true
}
}
}
return properties
}
func (t *Type) GetWithoutProperties() []*PropertyType {
var parentProps []*PropertyType
for _, p := range t.Extends {
parentProps = append(parentProps, p.GetWithoutProperties()...)
}
properties := make([]*PropertyType, 0, len(t.WithoutProperties)+len(parentProps))
set := make(map[string]bool)
for _, v := range t.Properties {
if !set[v.Name] {
properties = append(properties, v)
set[v.Name] = true
}
}
for _, v := range parentProps {
if !set[v.Name] {
properties = append(properties, v)
set[v.Name] = true
}
}
return properties
}
2018-01-24 08:00:59 +09:00
func (t *Type) AllExtendsNames() []string {
v := []string{t.Name}
for _, e := range t.Extends {
v = append(v, e.AllExtendsNames()...)
}
return v
}
type PropertyType struct {
Name string
URI string
Notes string
Domain []DomainReference
Range []RangeReference
// SubpropertyOf is ignorable as long as data is set up correctly
SubpropertyOf *PropertyType
Functional bool
NaturalLanguageMap bool
PreferIRIConvenience bool
}
type ValueType struct {
Name string
URI string
DefinitionType string
Zero string
ZeroValue string
DeserializeFn *FunctionDef
SerializeFn *FunctionDef
Imports []string
2018-01-24 08:00:59 +09:00
}
type DomainReference struct {
// One of:
T *Type
Any bool
}
type RangeReference struct {
// One of:
T *Type
V *ValueType
Any bool
}
func IsOnlyOtherPropertyBesidesIRI(nonIriIndex int, r []RangeReference) bool {
if len(r) != 2 {
return false
}
o := (nonIriIndex + 1) % 2
return r[o].V != nil && IsIRIValueType(r[o].V)
}