Example #1
0
func fillTreeWithEnum(tree *tree, key string, proto *descriptor.EnumDescriptorProto, loc string, locs map[string]*descriptor.SourceCodeInfo_Location) *enum {
	key = fmt.Sprintf("%s.%s", key, proto.GetName())

	tree.enums[key] = &enum{key: key, comment: getComment(loc, locs), EnumDescriptorProto: proto}

	// Values
	for idx, proto := range proto.GetValue() {
		tree.enums[key].values = append(tree.enums[key].values, &enumValue{
			getComment(fmt.Sprintf("%s,2,%d", loc, idx), locs),
			proto,
		})
	}

	return tree.enums[key]
}
Example #2
0
func (fg *FileGenerator) GenerateEnumDefinition(prefix string, inEnum *descriptor.EnumDescriptorProto) error {
	typeName := prefix + inEnum.GetName()
	fg.P("")
	fg.P("")
	fg.P("type %s", typeName)
	{
		fg.In()
		leading := "="
		for _, enumValue := range inEnum.GetValue() {
			// TODO: Convert names to CamelCase.
			fg.P("%s %s -- %d", leading, prefix+elmEnumValueName(enumValue.GetName()), enumValue.GetNumber())
			leading = "|"
		}
		fg.Out()
	}
	return nil
}
Example #3
0
func (fg *FileGenerator) GenerateEnumDecoder(prefix string, inEnum *descriptor.EnumDescriptorProto) error {
	typeName := prefix + inEnum.GetName()
	decoderName := decoderName(typeName)
	fg.P("")
	fg.P("")
	fg.P("%s : JD.Decoder %s", decoderName, typeName)
	fg.P("%s =", decoderName)
	{
		fg.In()
		fg.P("let")
		{
			fg.In()
			fg.P("lookup s =")
			fg.In()
			fg.P("case s of")
			{
				fg.In()
				for _, enumValue := range inEnum.GetValue() {
					fg.P("%q ->", enumValue.GetName())
					fg.In()
					fg.P("%s", prefix+elmEnumValueName(enumValue.GetName()))
					fg.P("")
					fg.Out()
				}
				// TODO: This should fail instead.
				fg.P("_ ->")
				fg.In()
				fg.P("%s", prefix+elmEnumValueName(inEnum.GetValue()[0].GetName()))
				fg.Out()
				fg.Out()
			}
			fg.Out()
			fg.Out()
		}
		fg.P("in")
		{
			fg.In()
			fg.P("JD.map lookup JD.string")
			fg.Out()
		}
		fg.Out()
	}

	defaultName := defaultEnumValue(typeName)
	fg.P("")
	fg.P("")
	fg.P("%s : %s", defaultName, typeName)
	fg.P("%s = %s", defaultName, prefix+elmEnumValueName(inEnum.GetValue()[0].GetName()))
	return nil
}
Example #4
0
func (fg *FileGenerator) GenerateEnumEncoder(prefix string, inEnum *descriptor.EnumDescriptorProto) error {
	typeName := prefix + inEnum.GetName()
	argName := "v"
	fg.P("")
	fg.P("")
	fg.P("%s : %s -> JE.Value", encoderName(typeName), typeName)
	fg.P("%s %s =", encoderName(typeName), argName)
	{
		fg.In()
		fg.P("let")
		{
			fg.In()
			fg.P("lookup s =")
			fg.In()
			fg.P("case s of")
			{
				fg.In()
				for _, enumValue := range inEnum.GetValue() {
					fg.P("%s ->", prefix+elmEnumValueName(enumValue.GetName()))
					fg.In()
					fg.P("%q", enumValue.GetName())
					fg.P("")
					fg.Out()
				}
				fg.Out()
			}
			fg.Out()
			fg.Out()
		}
		fg.P("in")
		{
			fg.In()
			fg.P("JE.string <| lookup %s", argName)
			fg.Out()
		}
		fg.Out()
	}
	return nil
}
func (p *protoBufErrors) lintProtoEnumType(
	pathIndex int32,
	pathType int32,
	parentPath []int32,
	protoEnum *descriptor.EnumDescriptorProto,
) {
	path := append(
		parentPath,
		pathType,
		pathIndex,
	)

	if !isCamelCase(protoEnum.GetName()) {
		p.addError(&protoBufError{
			path:        path,
			errorCode:   errorEnumTypeCase,
			errorString: protoEnum.GetName(),
		})
	}

	for i, v := range protoEnum.GetValue() {
		p.lintProtoEnumValue(int32(i), path, v)
	}
}