コード例 #1
0
ファイル: generate.go プロジェクト: arv/noms-old
// UserTypeJS returns a string containing the JS type that should be used when the Noms type described by t needs to be returned by a generated getter or taken as a parameter to a generated setter.
func (gen *Generator) UserTypeJS(t types.Type) string {
	rt := gen.R.Resolve(t)
	k := rt.Kind()
	switch k {
	case types.BlobKind:
		return gen.ImportJSType("Blob")
	case types.BoolKind:
		return "boolean"
	case types.StringKind:
		return "string"
	case types.Float32Kind, types.Float64Kind, types.Int16Kind, types.Int32Kind, types.Int64Kind, types.Int8Kind, types.Uint16Kind, types.Uint32Kind, types.Uint64Kind, types.Uint8Kind:
		return gen.ImportJSType(strings.ToLower(kindToString(k)))
	case types.EnumKind, types.StructKind:
		if t.HasPackageRef() {
			return gen.importedUserNameJS(t)
		}
		return gen.UserName(t)
	case types.ListKind:
		return fmt.Sprintf("%s<%s>", gen.ImportJSType("NomsList"), gen.UserTypeJS(t.Desc.(types.CompoundDesc).ElemTypes[0]))
	case types.SetKind:
		return fmt.Sprintf("%s<%s>", gen.ImportJSType("NomsSet"), gen.UserTypeJS(t.Desc.(types.CompoundDesc).ElemTypes[0]))
	case types.RefKind:
		return fmt.Sprintf("%s<%s>", gen.ImportJSType("RefValue"), gen.UserTypeJS(t.Desc.(types.CompoundDesc).ElemTypes[0]))
	case types.MapKind:
		elemTypes := t.Desc.(types.CompoundDesc).ElemTypes
		return fmt.Sprintf("%s<%s, %s>", gen.ImportJSType("NomsMap"), gen.UserTypeJS(elemTypes[0]), gen.UserTypeJS(elemTypes[1]))
	case types.PackageKind:
		return gen.ImportJSType("Package")
	case types.ValueKind:
		return gen.ImportJSType("Value")
	case types.TypeKind:
		return gen.ImportJSType("Type")
	}
	panic("unreachable")
}
コード例 #2
0
ファイル: codegen.go プロジェクト: arv/noms-old
func (gen *codeGen) resolveInPackage(t types.Type, p types.Package) (types.Type, types.Package) {
	d.Chk.True(t.IsUnresolved())

	// For unresolved types that references types in the same package the ref is empty and we need to use the passed in package.
	if t.HasPackageRef() {
		p = gen.deps[t.PackageRef()]
		d.Chk.NotNil(p)
	}

	return p.Types()[t.Ordinal()], p
}
コード例 #3
0
ファイル: codegen.go プロジェクト: arv/noms-old
func (gen *codeGen) Resolve(t types.Type) types.Type {
	if !t.IsUnresolved() {
		return t
	}
	if !t.HasPackageRef() {
		return gen.pkg.Types()[t.Ordinal()]
	}

	dep, ok := gen.deps[t.PackageRef()]
	d.Chk.True(ok, "Package %s is referenced in %+v, but is not a dependency.", t.PackageRef().String(), t)
	return dep.Types()[t.Ordinal()]
}
コード例 #4
0
ファイル: generate_test.go プロジェクト: arv/noms-old
func (res *testResolver) Resolve(t types.Type) types.Type {
	if !t.IsUnresolved() {
		return t
	}

	if !t.HasPackageRef() {
		res.assert.Fail("Test does not handle local references")
	}

	dep, ok := res.deps[t.PackageRef()]
	res.assert.True(ok, "Package %s is referenced in %+v, but is not a dependency.", t.PackageRef().String(), t)
	return dep.Types()[t.Ordinal()]
}
コード例 #5
0
ファイル: generate.go プロジェクト: arv/noms-old
// ToType returns a string containing Go code that instantiates a types.Type instance equivalent to t.
func (gen *Generator) ToType(t types.Type, fileID, packageName string) string {
	d.Chk.True(!t.HasPackageRef() && !t.IsUnresolved() || t.HasOrdinal(), "%s does not have an ordinal set", t.Name())
	if t.HasPackageRef() {
		d.Chk.True(t.HasOrdinal(), "%s does not have an ordinal set", t.Name())
		return fmt.Sprintf(`%sMakeType(ref.Parse("%s"), %d)`, gen.TypesPackage, t.PackageRef().String(), t.Ordinal())
	}

	if t.IsUnresolved() {
		if fileID != "" {
			return fmt.Sprintf(`%sMakeType(__%sPackageInFile_%s_CachedRef, %d)`, gen.TypesPackage, packageName, fileID, t.Ordinal())
		}

		d.Chk.True(t.HasOrdinal(), "%s does not have an ordinal set", t.Name())
		return fmt.Sprintf(`%sMakeType(ref.Ref{}, %d)`, gen.TypesPackage, t.Ordinal())
	}

	if types.IsPrimitiveKind(t.Kind()) {
		return fmt.Sprintf("%sMakePrimitiveType(%s%sKind)", gen.TypesPackage, gen.TypesPackage, kindToString(t.Kind()))
	}

	switch desc := t.Desc.(type) {
	case types.CompoundDesc:
		types := make([]string, len(desc.ElemTypes))
		for i, t := range desc.ElemTypes {
			types[i] = gen.ToType(t, fileID, packageName)
		}
		return fmt.Sprintf(`%sMakeCompoundType(%s%sKind, %s)`, gen.TypesPackage, gen.TypesPackage, kindToString(t.Kind()), strings.Join(types, ", "))
	case types.EnumDesc:
		return fmt.Sprintf(`%sMakeEnumType("%s", "%s")`, gen.TypesPackage, t.Name(), strings.Join(desc.IDs, `", "`))
	case types.StructDesc:
		flatten := func(f []types.Field) string {
			out := make([]string, 0, len(f))
			for _, field := range f {
				out = append(out, fmt.Sprintf(`%sField{"%s", %s, %t},`, gen.TypesPackage, field.Name, gen.ToType(field.T, fileID, packageName), field.Optional))
			}
			return strings.Join(out, "\n")
		}
		fields := fmt.Sprintf("[]%sField{\n%s\n}", gen.TypesPackage, flatten(desc.Fields))
		choices := fmt.Sprintf("%sChoices{\n%s\n}", gen.TypesPackage, flatten(desc.Union))
		return fmt.Sprintf("%sMakeStructType(\"%s\",\n%s,\n%s,\n)", gen.TypesPackage, t.Name(), fields, choices)
	default:
		d.Chk.Fail("Unknown TypeDesc.", "%#v (%T)", desc, desc)
	}
	panic("Unreachable")
}
コード例 #6
0
ファイル: generate.go プロジェクト: arv/noms-old
// ToTypeValueJS returns a string containing JS code that instantiates a Type instance equivalent to t for JavaScript.
func (gen *Generator) ToTypeValueJS(t types.Type, inPackageDef bool, indent int) string {
	d.Chk.True(!t.HasPackageRef() && !t.IsUnresolved() || t.HasOrdinal(), "%s does not have an ordinal set", t.Name())
	if t.HasPackageRef() {
		return fmt.Sprintf(`%s(%s.parse('%s'), %d)`, gen.ImportJS("makeType"), gen.ImportJS("Ref"), t.PackageRef().String(), t.Ordinal())
	}

	if t.IsUnresolved() {
		if inPackageDef {
			return fmt.Sprintf(`%s(new %s(), %d)`, gen.ImportJS("makeType"), gen.ImportJS("Ref"), t.Ordinal())
		}
		return fmt.Sprintf("%s(_pkg.ref, %d)", gen.ImportJS("makeType"), t.Ordinal())
	}

	if types.IsPrimitiveKind(t.Kind()) {
		return gen.ImportJS(firstToLower(kindToString(t.Kind())) + "Type")
	}

	switch desc := t.Desc.(type) {
	case types.CompoundDesc:
		types := make([]string, len(desc.ElemTypes))
		for i, t := range desc.ElemTypes {
			types[i] = gen.ToTypeValueJS(t, inPackageDef, 0)
		}
		return fmt.Sprintf(`%s(%s.%s, %s)`, gen.ImportJS("makeCompoundType"), gen.ImportJS("Kind"), kindToString(t.Kind()), strings.Join(types, ", "))
	case types.EnumDesc:
		return fmt.Sprintf(`%s('%s', '%s')`, gen.ImportJS("makeEnumType"), t.Name(), strings.Join(desc.IDs, `', '`))
	case types.StructDesc:
		flatten := func(f []types.Field) string {
			out := make([]string, 0, len(f))
			for _, field := range f {
				out = append(out, fmt.Sprintf(`%snew %s('%s', %s, %t),`, ind(indent+1), gen.ImportJS("Field"), field.Name, gen.ToTypeValueJS(field.T, inPackageDef, 0), field.Optional))
			}
			return strings.Join(out, "\n")
		}
		fields := fmt.Sprintf("%s[\n%s\n%s]", ind(indent), flatten(desc.Fields), ind(indent))
		choices := fmt.Sprintf("%s[\n%s\n%s]", ind(indent), flatten(desc.Union), ind(indent))
		return fmt.Sprintf("%s('%s',\n%s,\n%s\n%s)", gen.ImportJS("makeStructType"), t.Name(), fields, choices, ind(indent-1))
	default:
		d.Chk.Fail("Unknown TypeDesc.", "%#v (%T)", desc, desc)
	}
	panic("Unreachable")
}
コード例 #7
0
ファイル: generate.go プロジェクト: arv/noms-old
func (gen *Generator) refToID(t types.Type) string {
	if !t.IsUnresolved() || !t.HasPackageRef() {
		return gen.UserName(t)
	}
	return gen.UserName(gen.R.Resolve(t))
}
コード例 #8
0
ファイル: generate.go プロジェクト: arv/noms-old
func (gen Generator) importedUserNameJS(t types.Type) string {
	d.Chk.True(t.HasPackageRef())
	return fmt.Sprintf("%s.%s", gen.RefToAliasName(t.PackageRef()), gen.UserName(t))
}