func isEmptyValue(v schema.Value) bool { if !v.IsValid() { return false } switch v.Which() { case schema.Value_Which_text: b, _ := v.TextBytes() return len(b) == 0 case schema.Value_Which_data: b, _ := v.Data() return len(b) == 0 case schema.Value_Which_list: p, _ := v.ListPtr() return p.List().Len() == 0 default: return false } }
// uintValue returns the unsigned integer value of a schema value or // zero if the value is invalid (the null pointer). Panics if the value // is not an unsigned integer. func uintValue(v schema.Value) uint64 { if !v.IsValid() { return 0 } switch v.Which() { case schema.Value_Which_uint8: return uint64(v.Uint8()) case schema.Value_Which_uint16: return uint64(v.Uint16()) case schema.Value_Which_uint32: return uint64(v.Uint32()) case schema.Value_Which_uint64: return v.Uint64() } panic("unreachable") }
// Value formats a value from a schema (like a field default) as Go source. func (g *generator) Value(rel *node, t schema.Type, v schema.Value) (string, error) { if !isValueOfType(v, t) { return "", fmt.Errorf("value type is %v, but found %v value", t.Which(), v.Which()) } switch t.Which() { case schema.Type_Which_void: return "struct{}{}", nil case schema.Type_Which_interface: // The only statically representable interface value is null. return g.imports.Capnp() + ".Client(nil)", nil case schema.Type_Which_bool: if v.Bool() { return "true", nil } else { return "false", nil } case schema.Type_Which_uint8, schema.Type_Which_uint16, schema.Type_Which_uint32, schema.Type_Which_uint64: return fmt.Sprintf("uint%d(%d)", intbits(t.Which()), uintValue(v)), nil case schema.Type_Which_int8, schema.Type_Which_int16, schema.Type_Which_int32, schema.Type_Which_int64: return fmt.Sprintf("int%d(%d)", intbits(t.Which()), intValue(v)), nil case schema.Type_Which_float32: return fmt.Sprintf("%s.Float32frombits(0x%x)", g.imports.Math(), math.Float32bits(v.Float32())), nil case schema.Type_Which_float64: return fmt.Sprintf("%s.Float64frombits(0x%x)", g.imports.Math(), math.Float64bits(v.Float64())), nil case schema.Type_Which_text: text, _ := v.Text() return strconv.Quote(text), nil case schema.Type_Which_data: buf := make([]byte, 0, 1024) buf = append(buf, "[]byte{"...) data, _ := v.Data() for i, b := range data { if i > 0 { buf = append(buf, ',', ' ') } buf = strconv.AppendUint(buf, uint64(b), 10) } buf = append(buf, '}') return string(buf), nil case schema.Type_Which_enum: en := g.nodes[t.Enum().TypeId()] if en == nil || !en.IsValid() || en.Which() != schema.Node_Which_enum { return "", errors.New("expected enum type") } enums, _ := en.Enum().Enumerants() val := int(v.Enum()) if val >= enums.Len() { rn, err := g.RemoteNodeName(en, rel) if err != nil { return "", err } return fmt.Sprintf("%s(%d)", rn, val), nil } ev := makeEnumval(en, val, enums.At(val)) imp, err := importForNode(en, rel) if err != nil { return "", err } if imp.path == "" { return ev.FullName(), nil } qname := g.imports.add(imp) return qname + "." + ev.FullName(), nil case schema.Type_Which_structType: data, _ := v.StructValuePtr() var buf bytes.Buffer tn, err := g.nodes.mustFind(t.StructType().TypeId()) if err != nil { return "", err } sd, err := g.data.copyData(data) if err != nil { return "", err } err = templates.ExecuteTemplate(&buf, "structValue", structValueParams{ G: g, Node: rel, Typ: tn, Value: sd, }) return buf.String(), err case schema.Type_Which_anyPointer: data, _ := v.AnyPointerPtr() var buf bytes.Buffer sd, err := g.data.copyData(data) if err != nil { return "", err } err = templates.ExecuteTemplate(&buf, "pointerValue", pointerValueParams{ G: g, Value: sd, }) return buf.String(), err case schema.Type_Which_list: data, _ := v.ListPtr() var buf bytes.Buffer ftyp, err := g.RemoteTypeName(t, rel) if err != nil { return "", err } sd, err := g.data.copyData(data) if err != nil { return "", err } err = templates.ExecuteTemplate(&buf, "listValue", listValueParams{ G: g, Typ: ftyp, Value: sd, }) return buf.String(), err default: return "", fmt.Errorf("unhandled value type %v", t.Which()) } }
// intFieldDefaultMask returns the XOR mask used when getting or setting // signed integer struct fields. func intFieldDefaultMask(v schema.Value) uint64 { mask := uint64(1)<<intbits(schema.Type_Which(v.Which())) - 1 return uint64(intValue(v)) & mask }
func isValueOfType(v schema.Value, t schema.Type) bool { // Ensure that the value is for the given type. The schema ensures the union ordinals match. return !v.IsValid() || int(v.Which()) == int(t.Which()) }