Update for 0.19.0-rc.1
This commit is contained in:
		@@ -22,7 +22,6 @@ func (r *RoutesGenerator) Generate(routes []extractor.Route) error {
 | 
			
		||||
	f.HeaderComment("Code generated by go.elara.ws/go-lemmy/cmd/gen (routes generator). DO NOT EDIT.")
 | 
			
		||||
 | 
			
		||||
	for _, r := range routes {
 | 
			
		||||
 | 
			
		||||
		f.Comment(r.Summary)
 | 
			
		||||
		f.Func().Params(
 | 
			
		||||
			jen.Id("c").Id("*Client"),
 | 
			
		||||
@@ -37,27 +36,26 @@ func (r *RoutesGenerator) Generate(routes []extractor.Route) error {
 | 
			
		||||
			}
 | 
			
		||||
			g.Error()
 | 
			
		||||
		}).BlockFunc(func(g *jen.Group) {
 | 
			
		||||
			data := jen.Id("data")
 | 
			
		||||
			// If there are no parameters, set the data to nil
 | 
			
		||||
			if r.ParamsName == "" {
 | 
			
		||||
				data = jen.Nil()
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			returnName := r.ReturnName
 | 
			
		||||
			if returnName == "" {
 | 
			
		||||
				returnName = "EmptyResponse"
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if r.ParamsName == "" {
 | 
			
		||||
				g.Id("data").Op(":=").Qual("go.elara.ws/go-lemmy/types", "EmptyData").Block()
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			g.Id("resData").Op(":=").Op("&").Qual("go.elara.ws/go-lemmy/types", returnName).Block()
 | 
			
		||||
 | 
			
		||||
			var funcName string
 | 
			
		||||
			switch r.Method {
 | 
			
		||||
			case "GET":
 | 
			
		||||
			funcName := "req"
 | 
			
		||||
			if r.Method == "GET" {
 | 
			
		||||
				funcName = "getReq"
 | 
			
		||||
			default:
 | 
			
		||||
				funcName = "req"
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			g.List(jen.Id("res"), jen.Err()).Op(":=").Id("c").Dot(funcName).Params(
 | 
			
		||||
				jen.Id("ctx"), jen.Lit(r.Method), jen.Lit(r.Path), jen.Id("data"), jen.Op("&").Id("resData"),
 | 
			
		||||
				jen.Id("ctx"), jen.Lit(r.Method), jen.Lit(r.Path), data, jen.Op("&").Id("resData"),
 | 
			
		||||
			)
 | 
			
		||||
			g.If(jen.Err().Op("!=").Nil()).BlockFunc(func(g *jen.Group) {
 | 
			
		||||
				if returnName == "EmptyResponse" {
 | 
			
		||||
 
 | 
			
		||||
@@ -35,7 +35,7 @@ func (s *StructGenerator) Generate(items []extractor.Struct) error {
 | 
			
		||||
		} else {
 | 
			
		||||
			f.Type().Id(item.Name).StructFunc(func(g *jen.Group) {
 | 
			
		||||
				for _, field := range item.Fields {
 | 
			
		||||
					g.Id(transformFieldName(field.Name)).Id(getType(field)).Tag(map[string]string{
 | 
			
		||||
					g.Id(transformFieldName(field.Name)).Add(getType(field)).Tag(map[string]string{
 | 
			
		||||
						"json": field.Name,
 | 
			
		||||
						"url":  field.Name + ",omitempty",
 | 
			
		||||
					})
 | 
			
		||||
@@ -51,7 +51,19 @@ func (s *StructGenerator) Generate(items []extractor.Struct) error {
 | 
			
		||||
	return f.Render(s.w)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getType(f extractor.Field) string {
 | 
			
		||||
func getType(f extractor.Field) jen.Code {
 | 
			
		||||
	// Some time fields are strings in the JS client,
 | 
			
		||||
	// use time.Time for those
 | 
			
		||||
	switch f.Name {
 | 
			
		||||
	case "published", "updated", "when_":
 | 
			
		||||
		return jen.Qual("time", "Time")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Rank types such as hot_rank and hot_rank_active may be floats.
 | 
			
		||||
	if strings.Contains(f.Name, "rank") {
 | 
			
		||||
		return jen.Float64()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	t := transformType(f.Name, f.Type)
 | 
			
		||||
	if f.IsArray {
 | 
			
		||||
		t = "[]" + t
 | 
			
		||||
@@ -59,17 +71,10 @@ func getType(f extractor.Field) string {
 | 
			
		||||
	if f.IsOptional {
 | 
			
		||||
		t = "Optional[" + t + "]"
 | 
			
		||||
	}
 | 
			
		||||
	return t
 | 
			
		||||
	return jen.Id(t)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func transformType(name, t string) string {
 | 
			
		||||
	// Some time fields are strings in the JS client,
 | 
			
		||||
	// use LemmyTime for those
 | 
			
		||||
	switch name {
 | 
			
		||||
	case "published", "updated", "when_":
 | 
			
		||||
		return "LemmyTime"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	switch t {
 | 
			
		||||
	case "number":
 | 
			
		||||
		return "int64"
 | 
			
		||||
@@ -94,7 +99,6 @@ func transformFieldName(s string) string {
 | 
			
		||||
		"Png", "PNG",
 | 
			
		||||
		"Uuid", "UUID",
 | 
			
		||||
		"Wav", "WAV",
 | 
			
		||||
		"Ap", "AP",
 | 
			
		||||
	).Replace(s)
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user